简体   繁体   中英

IndexError: too many indices for array using numpy

I have trouble in solving this problem. I am using python numpy. And my goal is to print traffic light countdown.

EDIT: So it's like in 1 intersection, there are 8 stoplights, and 3 timings(red,amber,green). But my plus intersection consists of 4 intersection.

8 stoplights mean straight road, north to south straight road, south to north straight road, east to west straight road, west to east

left turn, north to west left turn, west to north left turn, north to east left turn, east to south

//edited
t = np.zeros((4, 8, 3)) //4 intersections, 8 stoplights, 3 timings

 for i in range(8):
   for j in range(4):
       t[j,i,0] = 10
       t[j,i,1] = 5
       t[j,i,2] = 10 

In the code stated above, it would give an error "Too many indices in array"

t[j,i,0] = 10
t[j,i,1] = 5
t[j,i,2] = 10

Can somebody please tell me why and how to solve this.

If you want to create a 3 dimensional array, probably you should be doing:

t = np.zeros((4,8,3))

You are actually creating a single dimensional array of elements 4,8,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM