Python: Return 2 ints for index in 2D lists given item

前端 未结 7 1141
轮回少年
轮回少年 2020-12-04 01:31

I\'ve been tinkering in python this week and I got stuck on something.
If I had a 2D list like this:

myList = [[1,2],[3,4],[5,6]]

and

相关标签:
7条回答
  • 2020-12-04 02:18

    Based on kevpie's answer I managed to get a 2D list containing the coordinates of all occurences

    myList = [[0,1],[1,1],[0,0],[1,0]]
    coordsList = [[x, y] for x, li in enumerate(myList) for y, val in enumerate(li) if val==1]
    

    Now coordsList contains all indexes for value 1 in myList :

    [[0, 1], [1, 0], [1, 1], [3, 0]]
    
    0 讨论(0)
提交回复
热议问题