Find all index of list containing a value Python

This question already has an answer here:

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

  • You might use something like

    ind = [[i for i, value in enumerate(lst) if value == x] for x in lst2]
    

    I don't know a purpose of this operation, but it might be more useful to use dict comprehension:

    ind = {x: [i for i, value in enumerate(lst) if value == x] for x in lst2}
    

    because in this case you will have output like that: {1: [0, 5, 7], 2: [1, 6, 8], 3: [2, 9]} , which might be easier to use.


    这就是你所要求的:

    ind = [i for i, x in enumerate(lst) for y in lst2 if x == y]
    
    链接地址: http://www.djcxy.com/p/24018.html

    上一篇: 如何在Python中显示列表元素的索引?

    下一篇: 查找包含值Python的列表的所有索引