#๐Ÿ”’ Doesn't let me assign one array to another

44 messages ยท Page 1 of 1 (latest)

novel basalt
#
import numpy as np
SIZE = 96
SHIFT = -SIZE/2

D2Q4 = np.zeros((SIZE + 1, SIZE + 1, 4))
D2Q4_new = D2Q4.copy()
D2Q5 = np.zeros((SIZE + 1, SIZE + 1, 5))
D2Q5_new = D2Q4.copy()
D2Q9 = np.zeros((SIZE + 1, SIZE + 1, 9))
D2Q9_new = D2Q4.copy()
velocity_model = [D2Q4, D2Q5, D2Q9]
velocity_model_new = [D2Q4_new, D2Q5_new, D2Q9_new]

for array in velocity_model:
    flag = velocity_model.index(array)
    print(len(velocity_model_new[flag][0][0]))
    print(len(array.copy()[0][0]))
    velocity_model_new[flag] = array.copy()
dense swiftBOT
#

@novel basalt

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

novel basalt
#

i get the following error

tulip jacinth
#
D2Q4_new = D2Q4.copy()
D2Q5 = np.zeros((SIZE + 1, SIZE + 1, 5))
D2Q5_new = D2Q5.copy()  # Corrected here
D2Q9 = np.zeros((SIZE + 1, SIZE + 1, 9))
D2Q9_new = D2Q9.copy()  # Corrected here```
#

You are copying the D2Q4 array to all three of them.

#

Change your code like that.

novel basalt
#

OOOPS

#

this doesn't help

#
import numpy as np
SIZE = 96
SHIFT = -SIZE/2

D2Q4 = np.zeros((SIZE + 1, SIZE + 1, 4))
D2Q4_new = D2Q4.copy()
D2Q5 = np.zeros((SIZE + 1, SIZE + 1, 5))
D2Q5_new = D2Q5.copy()
D2Q9 = np.zeros((SIZE + 1, SIZE + 1, 9))
D2Q9_new = D2Q9.copy()
velocity_model = [D2Q4, D2Q5, D2Q9]
velocity_model_new = [D2Q4_new, D2Q5_new, D2Q9_new]

print(id(D2Q4))
print(id(velocity_model[0]))
for array in velocity_model:
    print(id(array))
    flag = velocity_model.index(array)
    print(len(velocity_model_new[flag][0][0]))
    print(len(array.copy()[0][0]))
    velocity_model_new[flag] = array.copy()
still fog
#

!e ```py
import numpy as np
SIZE = 96
SHIFT = -SIZE/2

D2Q4 = np.zeros((SIZE + 1, SIZE + 1, 4))
D2Q4_new = D2Q4.copy()
D2Q5 = np.zeros((SIZE + 1, SIZE + 1, 5))
D2Q5_new = D2Q5.copy()
D2Q9 = np.zeros((SIZE + 1, SIZE + 1, 9))
D2Q9_new = D2Q9.copy()
velocity_model = [D2Q4, D2Q5, D2Q9]
velocity_model_new = [D2Q4_new, D2Q5_new, D2Q9_new]

print(id(D2Q4))
print(id(velocity_model[0]))
for array in velocity_model:
print(id(array))
flag = velocity_model.index(array)
print(flag)
print(len(velocity_model_new[flag][0][0]))
print(len(array.copy()[0][0]))
velocity_model_new[flag] = array.copy()

dense swiftBOT
#

@still fog :x: Your 3.12 eval job has completed with return code 1.

001 | 140422850182960
002 | 140422850182960
003 | 140422850182960
004 | 0
005 | 4
006 | 4
007 | 140422666139856
008 | Traceback (most recent call last):
009 |   File "/home/main.py", line 18, in <module>
010 |     flag = velocity_model.index(array)
011 |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/NDN7CQOVDCUTY57ADN7EFXUMSU

still fog
#

!e ```py
import numpy as np
SIZE = 96
SHIFT = -SIZE/2

D2Q4 = np.zeros((SIZE + 1, SIZE + 1, 4))
D2Q5 = np.zeros((SIZE + 1, SIZE + 1, 5))

print(D2Q4 == D2Q5)

dense swiftBOT
#

@still fog :x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 8, in <module>
003 |     print(D2Q4 == D2Q5)
004 |           ^^^^^^^^^^^^
005 | ValueError: operands could not be broadcast together with shapes (97,97,4) (97,97,5) 
still fog
#

I don't have the slighest idea of what you are trying to do, but put it simply, that is just not how you are supposed to work with numpy in first place

in particular, creating a list of array of different shapes is a bit weird, and trying to .index() it even more so

novel basalt
#

i need to iterate through them

still fog
#

What for?

novel basalt
#

to not make 3 instances of a function

tulip jacinth
#
    print(len(velocity_model_new[i][0][0]))
    print(len(velocity_model[i][0][0]))
    velocity_model_new[i] = velocity_model[i].copy()```
novel basalt
#

omg

#

its only way?

still fog
#

that (or enumerate) makes a lot more sense, but is still a bit weird

novel basalt
#

but wait

#

lets see if it even works

fallen fable
#

wait, enumerate is weird? Why?

still fog
novel basalt
#

because i need 3 grapth

#

graphs

#

i need to plot them all in 1 figure

still fog
#

yeah no, that makes even less sense to me

#

you're trying to plot something with shape (size, size, 9)?

novel basalt
#

i'm vectorizing it

#

doesn't matter

#

theres 3 matrixes

#

needed to be plotted at every step on the same figure

still fog
#

hmm ok, go ahead and use enumerate() then, if it works it works

novel basalt
#

not sure how enumerate works

#

using unicorns soution now

still fog
#

!e ```py
values = ['x', 'y', 'z']
for i, val in enumerate(values):
print(i)
print(val)

dense swiftBOT
#

@still fog :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 0
002 | x
003 | 1
004 | y
005 | 2
006 | z
still fog
#

forgot the range

novel basalt
#

ok now it works

#

idk why it doesn't like me passing arrays to iterate through

dense swiftBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.