How to return index of lists in Python

This question already has an answer here:

  • Accessing the index in 'for' loops? 17 answers

  • 使用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]

    链接地址: http://www.djcxy.com/p/24022.html

    上一篇: Python CSV阅读器:没有获得线路功能?

    下一篇: 如何在Python中返回列表的索引