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

前端 未结 12 1458
说谎
说谎 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:24

    Those list comprehensions are messy after a while.

    I like this Pythonic approach:

    from operator import itemgetter
    
    def collect(l, index):
       return map(itemgetter(index), l)
    
    # And now you can write this:
    collect(tuple_list,0).index("cherry")   # = 1
    collect(tuple_list,1).index("3")        # = 2
    

    If you need your code to be all super performant:

    # Stops iterating through the list as soon as it finds the value
    def getIndexOfTuple(l, index, value):
        for pos,t in enumerate(l):
            if t[index] == value:
                return pos
    
        # Matches behavior of list.index
        raise ValueError("list.index(x): x not in list")
    
    getIndexOfTuple(tuple_list, 0, "cherry")   # = 1
    
    0 讨论(0)
  • 2020-12-12 12:26

    Inspired by this question, I found this quite elegant:

    >>> tuple_list = [("pineapple", 5), ("cherry", 7), ("kumquat", 3), ("plum", 11)]
    >>> next(i for i, t in enumerate(tuple_list) if t[1] == 7)
    1
    >>> next(i for i, t in enumerate(tuple_list) if t[0] == "kumquat")
    2
    
    0 讨论(0)
  • 2020-12-12 12:26
    z = list(zip(*tuple_list))
    z[1][z[0].index('persimon')]
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-12 12:33

    No body suggest lambdas?

    Y try this and works. I come to this post searching answer. I don't found that I like, but I feel a insingth :P

        l #[['rana', 1, 1], ['pato', 1, 1], ['perro', 1, 1]]
    
        map(lambda x:x[0], l).index("pato") #1 
    

    Edit to add examples:

       l=[['rana', 1, 1], ['pato', 2, 1], ['perro', 1, 1], ['pato', 2, 2], ['pato', 2, 2]]
    

    extract all items by condition: filter(lambda x:x[0]=="pato", l) #[['pato', 2, 1], ['pato', 2, 2], ['pato', 2, 2]]

    extract all items by condition with index:

        >>> filter(lambda x:x[1][0]=="pato", enumerate(l))
        [(1, ['pato', 2, 1]), (3, ['pato', 2, 2]), (4, ['pato', 2, 2])]
        >>> map(lambda x:x[1],_)
        [['pato', 2, 1], ['pato', 2, 2], ['pato', 2, 2]]
    

    Note:_ variable only works in interactive interpreter y normal text file _ need explicti assign, ie _=filter(lambda x:x[1][0]=="pato", enumerate(l))

    0 讨论(0)
  • 2020-12-12 12:34

    Python's list.index(x) returns index of the first occurrence of x in the list. So we can pass objects returned by list compression to get their index.

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

    With the same line, we can also get the list of index in case there are multiple matched elements.

    >>> tuple_list = [("pineapple", 5), ("cherry", 7), ("kumquat", 3), ("plum", 11), ("banana", 7)]
    >>> [tuple_list.index(t) for t in tuple_list if t[1] == 7]
    [1, 4]
    
    0 讨论(0)
提交回复
热议问题