Using Python's list index() method on a list of tuples or objects?

前端 未结 12 1492
说谎
说谎 2020-12-12 12:15

Python\'s list type has an index() method that takes one parameter and returns the index of the first item in the list matching the parameter. For instance:



        
12条回答
  •  Happy的楠姐
    2020-12-12 12:33

    You can do this with a list comprehension and index()

    tuple_list = [("pineapple", 5), ("cherry", 7), ("kumquat", 3), ("plum", 11)]
    [x[0] for x in tuple_list].index("kumquat")
    2
    [x[1] for x in tuple_list].index(7)
    1
    

提交回复
热议问题