How to sort a list/tuple of lists/tuples by the element at a given index?

前端 未结 10 1694
南笙
南笙 2020-11-21 07:09

I have some data either in a list of lists or a list of tuples, like this:

data = [[1,2,3], [4,5,6], [7,8,9]]
data = [(1,2,3), (4,5,6), (7,8,9)]
相关标签:
10条回答
  • 2020-11-21 07:39

    @Stephen 's answer is to the point! Here is an example for better visualization,

    Shout out for the Ready Player One fans! =)

    >>> gunters = [('2044-04-05', 'parzival'), ('2044-04-07', 'aech'), ('2044-04-06', 'art3mis')]
    >>> gunters.sort(key=lambda tup: tup[0])
    >>> print gunters
    [('2044-04-05', 'parzival'), ('2044-04-06', 'art3mis'), ('2044-04-07', 'aech')]
    

    key is a function that will be called to transform the collection's items for comparison.. like compareTo method in Java.

    The parameter passed to key must be something that is callable. Here, the use of lambda creates an anonymous function (which is a callable).
    The syntax of lambda is the word lambda followed by a iterable name then a single block of code.

    Below example, we are sorting a list of tuple that holds the info abt time of certain event and actor name.

    We are sorting this list by time of event occurrence - which is the 0th element of a tuple.

    Note - s.sort([cmp[, key[, reverse]]]) sorts the items of s in place

    0 讨论(0)
  • 2020-11-21 07:40
    from operator import itemgetter
    data.sort(key=itemgetter(1))
    
    0 讨论(0)
  • 2020-11-21 07:40

    I just want to add to Stephen's answer if you want to sort the array from high to low, another way other than in the comments above is just to add this to the line:

    reverse = True
    

    and the result will be as follows:

    data.sort(key=lambda tup: tup[1], reverse=True)
    
    0 讨论(0)
  • 2020-11-21 07:40

    Without lambda:

    def sec_elem(s):
        return s[1]
    
    sorted(data, key=sec_elem)
    
    0 讨论(0)
  • 2020-11-21 07:44

    Stephen's answer is the one I'd use. For completeness, here's the DSU (decorate-sort-undecorate) pattern with list comprehensions:

    decorated = [(tup[1], tup) for tup in data]
    decorated.sort()
    undecorated = [tup for second, tup in decorated]
    

    Or, more tersely:

    [b for a,b in sorted((tup[1], tup) for tup in data)]
    

    As noted in the Python Sorting HowTo, this has been unnecessary since Python 2.4, when key functions became available.

    0 讨论(0)
  • 2020-11-21 07:45

    In order to sort a list of tuples (<word>, <count>), for count in descending order and word in alphabetical order:

    data = [
    ('betty', 1),
    ('bought', 1),
    ('a', 1),
    ('bit', 1),
    ('of', 1),
    ('butter', 2),
    ('but', 1),
    ('the', 1),
    ('was', 1),
    ('bitter', 1)]
    

    I use this method:

    sorted(data, key=lambda tup:(-tup[1], tup[0]))
    

    and it gives me the result:

    [('butter', 2),
    ('a', 1),
    ('betty', 1),
    ('bit', 1),
    ('bitter', 1),
    ('bought', 1),
    ('but', 1),
    ('of', 1),
    ('the', 1),
    ('was', 1)]
    
    0 讨论(0)
提交回复
热议问题