Finding the index of an element in nested lists in python

前端 未结 4 492
醉梦人生
醉梦人生 2020-12-10 09:25

I am trying to get the index of an element in nested lists in python - for example [[a, b, c], [d, e, f], [g,h]] (not all lists are the same size). I have tried

4条回答
  •  执笔经年
    2020-12-10 10:24

    suppose your list is like this:

    lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g','h']]
    list_no = 0
    pos = 0
    for x in range(0,len(lst)):
        try:
            pos = lst[x].index('e')
            break
        except:
            pass
    
    list_no = x
    

    list_no gives the list number and pos gives the position in that list

提交回复
热议问题