Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
index errors
#1
Can someone please tell what I am doing wrong??...I am continually getting an index error. I have a 4x2x3 matrix (lists) and I am trying to first create a list containing three lists and then as a final step I would like to reduce this to one list....I test lengths out in the command line so I do not understand why I am getting these index errors....
what would the solution look like in list comprehension?

thanks

import random
def m():
	c = []; tmpa = []; tmpb = []
	c.append([[[random.randint(0,4) for i in range(2)] for k in range(4)] for i in range(3)])
	for i in range(2):
		tmpa = []
		for j in range(4):
			tmpb = []
			for k in range(2):
				p = sum(c[i][j][k])
			tmpb.append(p)
		tmpb.append(tmpa)
	return(c)
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/pi/myPython/test.py", line 10, in m p = sum(c[i][j][k]) IndexError: list index out of range
Reply


Messages In This Thread
index errors - by ridgerunnersjw - Sep-14-2019, 06:42 PM
RE: index errors - by micseydel - Sep-14-2019, 07:15 PM
RE: index errors - by ridgerunnersjw - Sep-14-2019, 08:02 PM
RE: index errors - by ThomasL - Sep-14-2019, 09:10 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020