I want to write a function which will return indices of 2D array. I was guessing if I should return tuple or list. So I quickly tried to index 2D list using tuple and list:
>>> strw = [['1','2'],['3','4']]
>>> strw = [list(map(int, i)) for i in strw]
>>> strw
[[1, 2], [3, 4]]
Indexing above list using tuple gives error:
>>> tpl = (1,1)
>>> strw[tpl[0],tpl[1]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not tuple
Indexing list using another list also gives error:
>>> lst = [1,1]
>>> strw[lst[0],lst[1]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not tuple
Q1. How can I resolve this? Is there any standard approach to this (returning indexes from function)?
(Q2. Also somewhat unrelated question: how can I load file containing space separated ints into int array, instead of first creating string array with something like below and then mapping individual element to int
s:
with open(_file_path) as textFile:
strw = [line.split() for line in textFile]