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

前端 未结 12 1497
说谎
说谎 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条回答
  •  天命终不由人
    2020-12-12 12:46

    tuple_list = [("pineapple", 5), ("cherry", 7), ("kumquat", 3), ("plum", 11)]
    
    def eachtuple(tupple, pos1, val):
        for e in tupple:
            if e == val:
                return True
    
    for e in tuple_list:
        if eachtuple(e, 1, 7) is True:
            print tuple_list.index(e)
    
    for e in tuple_list:
        if eachtuple(e, 0, "kumquat") is True:
            print tuple_list.index(e)
    

提交回复
热议问题