KaC7
KaC7

Reputation: 23

How to do a lot of deep copies (Python)

I just found out how to make a deep copy of an array in python with the copy() command. Now I would like to find out how I can do several copies from the same array without wasting lots of lines of code

My original code (too long)

import numpy as np

a = np.zeros([3])
b = np.zeros([3])
c = np.zeros([3])
d = np.zeros([3])
e = np.zeros([3])
f = np.zeros([3])

Deep independant copies (still to long)

import numpy as np

a = np.zeros([3])
b=a.copy()
c=a.copy()
d=a.copy()
e=a.copy()
f=a.copy()

How can I do several deep copies with less commands? (Following command does shallow copies :P...)

import numpy as np

a = np.zeros([])
b = c = d = e = f = a.copy() 

Upvotes: 1

Views: 64

Answers (1)

mistiru
mistiru

Reputation: 3366

You could use a loop + list comprehension for that:

a, b, c, d, e, f = list(np.zeros([3]) for _ in range(6))

Upvotes: 3

Related Questions