I would like to create in python a list that inside has 3 lists, each of them have 2 vectors of 4 elements, something like that:
[[[0,0,0,0],[0,0,0,0]],
[[0,0,0,0],[0,0,0,0]],
[[0,0,0,0],[0,0,0,0]]]
This is just a simple example but I would like to automate it in order to be able to create an object like that with a much higher number of elements.
I tried to do it the following way:
grid=[np.zeros((2,4)) for x in range(3)]
grid
However when I print it the result is something like this instead
[array([[0., 0., 0., 0.],
[0., 0., 0., 0.]]), array([[0., 0., 0., 0.],
[0., 0., 0., 0.]]), array([[0., 0., 0., 0.],
[0., 0., 0., 0.]])]
The structure should be fine, but I don´t know if the fact that it says array is normal or I did something wrong.
Thanks in advance for your help
You have here a list of 2D np.array
, if you want a list
of list
of lists
:
grid = [[[0 for _ in range(4)] for _ in range(2)] for _ in range(3)]
The following will build the same structure, but each sublist will be copy, so unusable (see List of lists changes reflected )
grid = [[[0] * 4] * 2] * 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.