对具有多个条件的元组列表进行排序 [英] Sorting a list of tuples with multiple conditions

查看:168
本文介绍了对具有多个条件的元组列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试对以下列表进行排序:

list_ = [(1, '0101'), (1, '1010'), (1, '101'), (2, '01'), (2, '010'), (2, '10')]

这些是我要对其进行排序的步骤:

  1. 按元组第一个元素的值对列表进行排序
  2. 接下来,在步骤1完成后,按元组第二个元素的 length (不是值,长度!)对列表进行排序.
  3. 接下来,在步骤1和步骤2完成之后,按元组第二个元素的值对列表进行排序.

我的尝试:

sorted_by_length = sorted(list_, key=len x:x[1])

但是,我在key= len之后收到与x有关的语法错误. 在这种情况下,我应该使用什么正确的变量?

正确的排序列表应为:

sorted_by_length = [(1, '101'), (1, '0101'), (1, '1010'), (2, '01'), (2, '10'), (2, '010')]

感谢您的帮助.

解决方案

key函数可以返回一个元组.

sorted_by_length = sorted(list_,
                         key=lambda x: (x[0], len(x[1]), float(x[1])))

之所以可行,是因为在字典上按顺序对元组进行了排序:(元组的第一个元素首先用于排序,然后第二个元素用于打破联系,然后第三个元素用于打破任何剩余的联系.)

请参阅出色的 HOWTO Sort ,以获取有关此问题以及与之相关的其他问题的说明排序.


In [1]: list_ = [(1, '0101'), (1, '1010'), (1, '101'), (2, '01'), (2, '010'), (2, '10')]

In [2]: sorted_by_length = sorted(list_,
                         key=lambda x: (x[0], len(x[1]), float(x[1])))
   ...: 
In [3]: sorted_by_length
Out[3]: [(1, '101'), (1, '0101'), (1, '1010'), (2, '01'), (2, '10'), (2, '010')]


如果每个元组的第二个元素是二进制形式的int的字符串表示形式,则在排序键中使用int(x, 2)而不是float(x).如果要将它们用作整数的十进制表示形式,请使用int(x).

I am currently trying to sort the following list:

list_ = [(1, '0101'), (1, '1010'), (1, '101'), (2, '01'), (2, '010'), (2, '10')]

These are the steps I want to take in order to sort it:

  1. Sort the list by the value of the first element of the tuples
  2. Next, sort the list by the length of the second element of the tuples (not the value, the length!) AFTER step 1 finishes.
  3. Next, sort the list by the value of the second element of the tuples AFTER step 1 and step 2 finishes.

My attempt:

sorted_by_length = sorted(list_, key=len x:x[1])

However, I received a syntax error concerning the x after key= len. What is the right variable I should be using in this case?

The correct, sorted list should be:

sorted_by_length = [(1, '101'), (1, '0101'), (1, '1010'), (2, '01'), (2, '10'), (2, '010')]

Thank you for help.

解决方案

The key function can return a tuple.

sorted_by_length = sorted(list_,
                         key=lambda x: (x[0], len(x[1]), float(x[1])))

This works because tuples are sorted lexicographically: (the first element of the tuple is used for sorting first, then the second element is used for breaking ties, and then the third element is used for breaking any remaining ties.)

See the excellent HOWTO Sort for an explanation of this and other issues related to sorting.


In [1]: list_ = [(1, '0101'), (1, '1010'), (1, '101'), (2, '01'), (2, '010'), (2, '10')]

In [2]: sorted_by_length = sorted(list_,
                         key=lambda x: (x[0], len(x[1]), float(x[1])))
   ...: 
In [3]: sorted_by_length
Out[3]: [(1, '101'), (1, '0101'), (1, '1010'), (2, '01'), (2, '10'), (2, '010')]


If the second element of each tuple is the string representation of an int in binary, then use int(x, 2) instead of float(x) in the sort key. If they are intended to be the decimal representation of an integer, then use int(x).

这篇关于对具有多个条件的元组列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆