#๐ List errors
14 messages ยท Page 1 of 1 (latest)
@thorn jolt
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.
positions = [[]] * int((WID*HEI)/256+160) #length of 4160
for mov in particles:
t = mov.update(WID,HEI) # t is an int (and can only possibly be an int)
positions[t].append(mov) # shouldn't this only modify one sublist??
#This line for some reason adds "mov" to every sublist in "positions"
#When it should only add to one sublist (denoted by "t")
You have created a list where each item contains the same object. Updating one object updates all of them.
You should use a list comprehension with range() or look at numpy
positions = [[] for _ in range(length)]
got it
for numpy, you just wrap what you currently have in np.array([[0] * HEI] * WID)
this worked, thx
!e ```py
import numpy as np
ar = np.array([[0] * 10] * 10)
ar[0,0] = 1
print(ar)
@dense kettle :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | [[1 0 0 0 0 0 0 0 0 0]
002 | [0 0 0 0 0 0 0 0 0 0]
003 | [0 0 0 0 0 0 0 0 0 0]
004 | [0 0 0 0 0 0 0 0 0 0]
005 | [0 0 0 0 0 0 0 0 0 0]
006 | [0 0 0 0 0 0 0 0 0 0]
007 | [0 0 0 0 0 0 0 0 0 0]
008 | [0 0 0 0 0 0 0 0 0 0]
009 | [0 0 0 0 0 0 0 0 0 0]
010 | [0 0 0 0 0 0 0 0 0 0]]
the expression
int((WID*HEI)/256+160)
can be simplified to
WID*HEI // 256 + 160
the // operator is a blessing, don't leave integer space unless strictly necessary
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.