Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

378
Visualizações
Copying a list of numpy arrays

I am working with (lists of) lists of numpy arrays. As a bare bones example, consider this piece of code:

a = [np.zeros(5)]
b = a.copy()
b[0] += 1

Here, I copy a list of one array from a to b. However, the array itself is not copied, so:

print(a)
print(b)

both give [array([1., 1., 1., 1., 1.])]. If I want to make a copy of the array as well, I could do something like:

b = [arr.copy() for arr in a]

and a would remain unchanged. This works well for a simple list, but it becomes more complicated when working with nested lists of arrays where the number of arrays in each list is not always the same.

Is there a simple way to copy a multi-level list and every object that it contains without keeping references to the objects in the original list? Basically, I would like to avoid nested loops as well as dealing with the size of each individual sub-list.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

What you are looking for is a deepcopy

import numpy as np
import copy
a = [np.zeros(5)]
b = copy.deepcopy(a)
b[0] += 1  # a[0] is not changed

This is actually method recommended in numpy doc for the deepcopy of object arrays.

over 4 years ago · Santiago Trujillo Relatório

0

You need to use deepcopy.

import numpy as np
import copy

a = [np.zeros(5)]
b = copy.deepcopy(a)
b[0] += 1

print(a)
print(b)

Result:

[array([0., 0., 0., 0., 0.])]
[array([1., 1., 1., 1., 1.])]
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda