Finding the index of an element in nested lists in python

前端 未结 4 491
醉梦人生
醉梦人生 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:08

    does this suffice?

    array = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']]
    for subarray in array:
        if 'a' in subarray:
            print(array.index(subarray), '-', subarray.index('a'))
    

    This will return 0 - 0. First zero is the index of the subarray inside array, and the last zero is the 'a' index inside subarray.

提交回复
热议问题