Im trying to create a 3D-array (8,15,8). I manage to create the array but how to asign values to at specific index?
Got these messages:
File "G:\Min enhet\Python\Travscript.py", line 41, in start_list_array[int(1),int(1),int(1)]=attribute.strip()
TypeError: list indices must be integers or slices, not tuple
My code:
start_list_array = []
for i in range(8):
start_list_array.append([])
for j in range(15):
start_list_array[i].append([])
for k in range(8):
start_list_array[i][j].append([])
line_count = 0
race_count = 0
horse_count = 0
attribute_count = 0
for line in input:
line_count = 1
if line.strip() in spelformer:
race_count = 1
elif line_count == 5:
horse_count = 1
for attribute in line.split('\t'):
attribute_count = 1
print(line_count)
print(race_count)
print(attribute_count)
#start_list_array[int(race_count-1), int(horse_count-1), int(attribute_count-1)] = attribute.strip()
start_list_array[int(1),int(1),int(1)]=attribute.strip()
print("check2")
print(start_list_array[int(race_count-1), int(horse_count-1), int(attribute_count-1)])
Thanks in advance.
CodePudding user response:
Vanilla lists in Python need to be accessed this way:
start_list_array[1][1][1]
The __get_item__
method in Python is implemented separately for each class and while other objects such as numpy
arrays or pandas
DataFrames may have implementations that support tuples, the list
object does not.
In fact, your top-level list is agnostic to what it has inside it, for all it knows it could be strings or lists of different lengths.