using index() on multidimensional lists

前端 未结 8 2042
北恋
北恋 2020-12-10 03:00

For a one dimensional list, the index of an item is found as follows:

 a_list = [\'a\', \'b\', \'new\', \'mpilgrim\', \'new\']
 a_list.index(\'mpilgrim\')


        
相关标签:
8条回答
  • 2020-12-10 03:55

    A multidimensional list is simply a list with more lists inside of it. So its indices would be lists themselves.

    a = [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
    print a.index([2, 3, 4])
    # prints 1
    
    0 讨论(0)
  • 2020-12-10 03:59

    If you want to find the list that has an item, the simplest way to do it is:

    i = 4
    index = b_list[0].index( filter(lambda 1D_list: i in index , b_list[0]) )
    

    Or if you know there are more than one matches for the item, then you can do:

    i = 4
    indexes = []
    for match in filter(lambda 1D_list: i in list, b_list[0]):
        indexes.append(b_list[0].index(match))
    

    None of this will raise any errors but they'll only work if there is no subarray. Go here for information about the functionality of filter.

    0 讨论(0)
提交回复
热议问题