Tuesday, 15 July 2014

Python : Loop structure occurs error like "list indices must be integers, not tuple" -



Python : Loop structure occurs error like "list indices must be integers, not tuple" -

uil=[[1], [2], [3, 3, 1], [2, 3], [1], [2]] in uil: j in uil[i]: print(uil[i][j])

it reports error that: "list indices must integers, not tuple".

however, not work if add together int() uil[i]. btw, hope code simplified 1 line , more efficient process 1 1000000 tuples.

how can revise it?

i not in index, the nested list itself. loop straight on it. same j:

for in uil: j in i: print(j)

the python for statement for each construct.

if want process each nested value in sequence, no reference grouping lists, utilize itertools.chain.from_iterable() 'flatten' list:

from itertools import chain j in chain.from_iterable(uil): print(j)

python indices

No comments:

Post a Comment