-1

How to write this in cycle, please?

k1 = np.empty(np.shape(u))
k2 = np.empty(np.shape(u))
k3 = np.empty(np.shape(u))
k4 = np.empty(np.shape(u))

I tried:

 k = [k1, k2, k3, k4]

    for i in k:
        i = np.empty(np.shape(u))
    k.append(i)
Alex
  • 347
  • 1
  • 10
  • What do you want to accomplish? Get a list of four arrays? Or four arrays as separate variables? (The latter is probably not what you _really_ want.) – DYZ May 03 '20 at 06:35
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – DYZ May 03 '20 at 06:36

2 Answers2

1

You can simply use list comprehension to create an arbitrary number of empty numpy arrays

num = 10
result = [np.empty(np.shape(u)) for _ in range(num)]
A Kareem
  • 586
  • 3
  • 10
1

It is not a good practice to do this, so I would recommend using lists or dictionaries but here's the code to achieve what you asked for-

for x in range(0, n): #Replace n with the value you need
    globals()['k%s' % x] = np.empty(np.shape(u))

and then for example:

print(k1)

But again this is a bad practice, use dictionaries instead

Rishit Dagli
  • 1,000
  • 8
  • 20