How to return index of lists in Python
This question already has an answer here:
使用enumerate
:
>>> a = [[0,1,2],[0,0,0],[1,1,1]]
>>> [i for i, x in enumerate(a, 1) if 2 not in x]
[2, 3]
Use enumerate
:
[i for i, j in enumerate(a) if 2 not in j]
note that list indices in Python start from 0
, not from 1
你可以使用index [U.index(l) for l in U if not 2 in l]
下一篇: 如何在Python中返回列表的索引