Python排序问题
我需要在Python中对以下元组列表进行排序:
ListOfTuples = [('10', '2010 Jan 1;', 'Rapoport AM', 'Role of antiepileptic drugs as preventive agents for migraine', '20030417'), ('21', '2009 Nov;', 'Johannessen SI', 'Antiepilepticdrugs in epilepsy and other disorders--a population-based study of prescriptions', '19679449'),...]
我的目的是按降序年份(listOfTuples[2])和升序作者(listOfTuples[2])排序):
sorted(result, key = lambda item: (item[1], item[2]))
但这不起作用。如何获得排序稳定性?
i need to sort the following list of Tuples in Python:
ListOfTuples = [('10', '2010 Jan 1;', 'Rapoport AM', 'Role of antiepileptic drugs as preventive agents for migraine', '20030417'), ('21', '2009 Nov;', 'Johannessen SI', 'Antiepilepticdrugs in epilepsy and other disorders--a population-based study of prescriptions', '19679449'),...]
My purpose is to order it by Descending year (listOfTuples[2]) and by Ascending Author (listOfTuples[2]):
sorted(result, key = lambda item: (item[1], item[2]))
But it doesn't work. How can i obtain sort stability?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
注意:您需要将年份提取为整数(而不是字符串),以便您可以更改其符号 - 后者是满足规范“降序”部分的关键技巧。将所有内容压缩到 lambda 中是可能的,但是绝对没有理由这样做并牺牲更多的可读性,因为
def 也可以正常工作(而且更多)可读)。
Notes: you need to extract the year as an integer (not as a string), so that you can change its sign -- the latter being the key trick in order to satisfy the "descending" part of the specifications. Squeezing it all within a
lambda
would be possible, but there's absolutely no reason to do so and sacrifice even more readability, when adef
will work just as well (and far more readably).最简单的方法是分别对每个键值进行排序。从最不重要的键开始,一直到最重要的键。
所以在这种情况下:
这是有效的,因为即使你使用反向标志,Python 的排序也总是稳定的:即反向不只是排序然后反向(这会失去稳定性,它在反向后保持稳定性。
当然,如果你有很多对于关键列,这可能效率很低,因为它会多次进行完整排序。
您不必以这种方式将年份转换为数字,因为它是真正的反向排序,但如果您愿意,也可以这样做。
The easiest way is to sort on each key value separately. Start at the least significant key and work your way up to the most significant.
So in this case:
This works because Python's sorting is always stable even when you use the reverse flag: i.e. reverse doesn't just sort and then reverse (which would lose stability, it preserves stability after reversing.
Of course if you have a lot of key columns this can be inefficient as it does a full sort several times.
You don't have to convert the year to a number this way as its a genuine reverse sort, though you could if you wanted.
这是一个适用于所有事物的习惯用法,甚至是你无法否定的事物,例如字符串:
Here is a idiom that works for everything, even thing you can't negate, for example strings:
这是一个粗略的解决方案,需要考虑月份缩写和日期(如果找到):
“%b”是语言环境的缩写月份名称,如果您不想处理语言环境,可以使用字典。
Here's a rough solution that takes month abbreviature and day (if found) in account:
"%b" is locale's abbreviated month name, you can use a dictionary if you prefer not to deal with locales.
这是 Alex 答案的 lambda 版本。我认为它现在看起来比邓肯的答案更紧凑,但显然亚历克斯的答案已经失去了很多可读性。
可读性和效率通常应该优先于紧凑性。
Here is the lambda version of Alex's answer. I think it looks more compact than Duncan's answer now, but obviously a lot of the readability of Alex's answer has been lost.
Readability and efficiency should usually be preferred to compactness.