I am new to python 3 and trying to work with a list but struggling. I have read things but some are quite confusing, so I have been playing with code but getting nowhere fast.
I want to store 3 elements (Count,Angle,Speed) for every second of the day which is 86400 entries.
To make things easier for this example lets have a list of 2 entries of 3 elements I created a list and initially want to set every element to 0
a = [[0, 0, 0], [0, 0, 0]]
print(a[0])
print(a[0][0])
print(a[0][1])
print(a[0][2])
print(a[1])
print(a[1][0])
print(a[1][1])
print(a[1][2])
print("** Change a[0][2] to 7 from 0")
a[0][2] = 7
print(a[0])
print(a[0][0])
print(a[0][1])
print(a[0][2])
print("-----------")
print(a[1])
print(a[1][0])
print(a[1][1])
print(a[1][2])
print("-----------")
This works great and for example I am able to access and change a[0][2]
[0, 0, 0] 0 0 0 [0, 0, 0] 0 0 0 ** Change a[0][2] to 7 from 0 [0, 0, 7] 0 0 7
[0, 0, 0] 0 0 0
However I want to be able to pre-fill the list with 0 as it will be tedious to do it for 86400 entries but again for this example lets go with 2
So I read up and seen how to do it using... a = [[0,0,0]] * 2
a = [[0,0,0]] * 2
print(a[0])
print(a[0][0])
print(a[0][1])
print(a[0][2])
print(a[1])
print(a[1][0])
print(a[1][1])
print(a[1][2])
print("** Change a[0][2] to 7 from 0")
a[0][2] = 7
print(a[0])
print(a[0][0])
print(a[0][1])
print(a[0][2])
print("-----------")
print(a[1])
print(a[1][0])
print(a[1][1])
print(a[1][2])
print("-----------")
However why does a[1][2] also change to 7
[0, 0, 0] 0 0 0 [0, 0, 0] 0 0 0 ** Change a[0][2] to 7 from 0 [0, 0, 7] 0 0 7
[0, 0, 7] 0 0 7
I have obviously done something wrong with pre-filling the list or even defining the list, so if anybody can help I would be grateful.
Thanks
CodePudding user response:
[[0,0,0]]*2
means that you evaluate [0,0,0]
, that is build such a list. And then create an array of 2 elements whose each element is this [0,0,0]
. So it is the same one!
It is like doing
arr=[]
arr0=[0,0,0]
for i in range(2):
arr.append(arr0)
So, since arr[0]
and arr[1]
are the same list, altering one, alters the other.
Note that changing arr[0]
does not alter arr[1]
. Because that is changing the content of arr
not of arr[0]
.
So
arr=[[0,0,0]]*2
arr[0]=12
# arr = [12, [0,0,0]], not [12,12]
What you need to do is to build a new [0,0,0]
for each line.
For example like this
arr=[ [0]*3 for _ in range(2) ]
arr[0][2]=7
# [[0, 0, 7], [0, 0, 0]]
Or, alternative, using numpy
import numpy as np
arr=np.zeros((86400, 3))
arr[0,2]=7
CodePudding user response:
You can simply use numpy to create array of dimension 86400, 3
import numpy as np
a=np.zeros((86400, 3))
CodePudding user response:
It's a good idea to use the numpy module for big numerical arrays as access is very efficient - worth reading up on. You can easily create a numpy array of any shape and fill with zeros (ints or floats)
import numpy as np
x = np.zeros((86400, 3), dtype = int)
CodePudding user response:
You can create a list of list 86400 x 3
like this.
l = [[0]*3 for _ in range(86400)]