如何按给定索引处的元素对列表/元组进行排序(列表/元组)?

时间:2021-10-14 00:30:56

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)]

And I want to sort by the 2nd element in the subset. Meaning, sorting by 2,5,8 where 2 is from (1,2,3), 5 is from (4,5,6). What is the common way to do this? Should I store tuples or lists in my list?

我想按子集中的第二个元素排序。意思是,按2,5,8排序,其中2来自(1,2,3),5来自(4,5,6)。这样做的常用方法是什么?我应该在列表中存储元组或列表吗?

10 个解决方案

#1


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

or:

data.sort(key=lambda tup: tup[1])  # sorts in place

#2


from operator import itemgetterdata.sort(key=itemgetter(1))

#3


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:

我只想添加到Stephen的答案,如果你想将数组从高到低排序,除了上面的评论之外的另一种方法只是将其添加到该行:

reverse = True

and the result will be as follows:

结果如下:

data.sort(key=lambda tup: tup[1], reverse=True)

#4


For sorting by multiple criteria, namely for instance by the second and third elements in a tuple, let

对于按多个标准排序,即例如通过元组中的第二个和第三个元素,让

data = [(1,2,3),(1,2,1),(1,1,4)]

and so define a lambda that returns a tuple that describes priority, for instance

因此,定义一个lambda,它返回一个描述优先级的元组

sorted(data, key=lambda tup: (tup[1],tup[2]) )[(1, 1, 4), (1, 2, 1), (1, 2, 3)]

#5


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

斯蒂芬的答案是我使用的答案。为了完整性,这里是带有列表推导的DSU(decorate-sort-undecorate)模式:

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.

正如Python Sorting HowTo所述,自从Python 2.4开始提供关键功能以来,这是不必要的。

#6


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)]

#7


Without lambda:

def sec_elem(s):    return s[1] 
sorted(data, key=sec_elem) 

#8


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

@Stephen的回答是关键!这是一个更好的可视化的例子,

Shout out for the Ready Player One fans! =)

为Ready Player One粉丝大喊大叫! =)

>>> 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.

key是一个函数,它将被调用以转换集合的项目以进行比较..就像Java中的compareTo方法一样。

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.

传递给key的参数必须是可调用的。这里,lambda的使用创建了一个匿名函数(可以调用)。 lambda的语法是单词lambda,后跟可迭代的名称,然后是单个代码块。

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.

我们按事件发生的时间对此列表进行排序 - 这是元组的第0个元素。

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

注 - s.sort([cmp [,key [,reverse]]])对s的项目进行排序

#9


itemgetter() is somewhat faster than lambda tup: tup[1], but the increase is relatively modest (around 10 to 25 percent).

itemgetter()比lambda tup:tup [1]快一些,但增长相对适中(约10%到25%)。

(IPython session)

>>> from operator import itemgetter>>> from numpy.random import randint>>> values = randint(0, 9, 30000).reshape((10000,3))>>> tpls = [tuple(values[i,:]) for i in range(len(values))]>>> tpls[:5]    # display sample from list[(1, 0, 0),  (8, 5, 5),  (5, 4, 0),  (5, 7, 7),  (4, 2, 1)]>>> sorted(tpls[:5], key=itemgetter(1))    # example sort[(1, 0, 0),  (4, 2, 1),  (5, 4, 0),  (8, 5, 5),  (5, 7, 7)]>>> %timeit sorted(tpls, key=itemgetter(1))100 loops, best of 3: 4.89 ms per loop>>> %timeit sorted(tpls, key=lambda tup: tup[1])100 loops, best of 3: 6.39 ms per loop>>> %timeit sorted(tpls, key=(itemgetter(1,0)))100 loops, best of 3: 16.1 ms per loop>>> %timeit sorted(tpls, key=lambda tup: (tup[1], tup[0]))100 loops, best of 3: 17.1 ms per loop

#10


Sorting a tuple is quite simple:

对元组进行排序非常简单:

tuple(sorted(t))

#1


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

or:

data.sort(key=lambda tup: tup[1])  # sorts in place

#2


from operator import itemgetterdata.sort(key=itemgetter(1))

#3


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:

我只想添加到Stephen的答案,如果你想将数组从高到低排序,除了上面的评论之外的另一种方法只是将其添加到该行:

reverse = True

and the result will be as follows:

结果如下:

data.sort(key=lambda tup: tup[1], reverse=True)

#4


For sorting by multiple criteria, namely for instance by the second and third elements in a tuple, let

对于按多个标准排序,即例如通过元组中的第二个和第三个元素,让

data = [(1,2,3),(1,2,1),(1,1,4)]

and so define a lambda that returns a tuple that describes priority, for instance

因此,定义一个lambda,它返回一个描述优先级的元组

sorted(data, key=lambda tup: (tup[1],tup[2]) )[(1, 1, 4), (1, 2, 1), (1, 2, 3)]

#5


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

斯蒂芬的答案是我使用的答案。为了完整性,这里是带有列表推导的DSU(decorate-sort-undecorate)模式:

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.

正如Python Sorting HowTo所述,自从Python 2.4开始提供关键功能以来,这是不必要的。

#6


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)]

#7


Without lambda:

def sec_elem(s):    return s[1] 
sorted(data, key=sec_elem) 

#8


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

@Stephen的回答是关键!这是一个更好的可视化的例子,

Shout out for the Ready Player One fans! =)

为Ready Player One粉丝大喊大叫! =)

>>> 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.

key是一个函数,它将被调用以转换集合的项目以进行比较..就像Java中的compareTo方法一样。

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.

传递给key的参数必须是可调用的。这里,lambda的使用创建了一个匿名函数(可以调用)。 lambda的语法是单词lambda,后跟可迭代的名称,然后是单个代码块。

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.

我们按事件发生的时间对此列表进行排序 - 这是元组的第0个元素。

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

注 - s.sort([cmp [,key [,reverse]]])对s的项目进行排序

#9


itemgetter() is somewhat faster than lambda tup: tup[1], but the increase is relatively modest (around 10 to 25 percent).

itemgetter()比lambda tup:tup [1]快一些,但增长相对适中(约10%到25%)。

(IPython session)

>>> from operator import itemgetter>>> from numpy.random import randint>>> values = randint(0, 9, 30000).reshape((10000,3))>>> tpls = [tuple(values[i,:]) for i in range(len(values))]>>> tpls[:5]    # display sample from list[(1, 0, 0),  (8, 5, 5),  (5, 4, 0),  (5, 7, 7),  (4, 2, 1)]>>> sorted(tpls[:5], key=itemgetter(1))    # example sort[(1, 0, 0),  (4, 2, 1),  (5, 4, 0),  (8, 5, 5),  (5, 7, 7)]>>> %timeit sorted(tpls, key=itemgetter(1))100 loops, best of 3: 4.89 ms per loop>>> %timeit sorted(tpls, key=lambda tup: tup[1])100 loops, best of 3: 6.39 ms per loop>>> %timeit sorted(tpls, key=(itemgetter(1,0)))100 loops, best of 3: 16.1 ms per loop>>> %timeit sorted(tpls, key=lambda tup: (tup[1], tup[0]))100 loops, best of 3: 17.1 ms per loop

#10


Sorting a tuple is quite simple:

对元组进行排序非常简单:

tuple(sorted(t))