简体   繁体   English

元组的python排序列表

[英]python sort list of tuple

I am trying to sorting a list of tuple.我正在尝试对元组列表进行排序。 for example, If例如,如果

>>>recommendations = [('Gloria Pritchett', 2), ('Manny Delgado', 1), ('Cameron Tucker', 1), ('Luke Dunphy', 3)] 

I want to get我想得到

Luke Dunphy
Gloria Pritchett
Cameron Tucker
Manny Delgado

This is what I did:这就是我所做的:

This code only gives me这段代码只给了我

>>> [('Luke Dunphy', 3), ('Gloria Pritchett', 2), ('Cameron Tucker', 1), ('Manny Delgado', 1)]

I have no idea how to append only names(strings) in sorted_list.我不知道如何在 sorted_list 中只附加名称(字符串)。 Please help!请帮忙!

You can pass in the key to sorted:您可以将键传递给 sorted:

>>> s = sorted(recommendations, key=lambda x: x[1], reverse=True)
[('Luke Dunphy', 3), ('Gloria Pritchett', 2), ('Manny Delgado', 1), ('Cameron Tucker', 1)]

Then get the names:然后获取名称:

names = [x[0] for x in s]
# ['Luke Dunphy', 'Gloria Pritchett', 'Manny Delgado', 'Cameron Tucker']

If you've noticed, Manny Delgado and Cameron Tucker are tied based on their key(1), but Manny Delgado comes before Cameron Tucker, because python sorting is in-place .如果您已经注意到,Manny Delgado 和 Cameron Tucker 基于他们的 key(1) 并列,但 Manny Delgado 排在 Cameron Tucker 之前,因为 Python 排序是就地的 However, based on your desired output, you want the ties in primary key to be resolved using the secondary key (the name in this case).但是,根据所需的输出,您希望使用辅助键(在本例中为名称)解析主键中的关系。 You can do this by first sorting by name and then sorting by the primary integer key:您可以先按名称排序,然后由主整数键排序做到这一点:

t = sorted(recommendations, key=lambda x: x[0])
s = sorted(t, key=lambda x: x[1], reverse=True)
# [('Luke Dunphy', 3), ('Gloria Pritchett', 2), ('Cameron Tucker', 1), ('Manny Delgado', 1)]

Note that Cameron Tucker comes before Manny Delgado now.请注意,Cameron Tucker 现在排在 Manny Delgado 之前。 All this and more is covered in detail in the excellent Sorting Howto所有这些以及更多内容都在优秀的Sorting Howto中详细介绍

First of all, it sounds like you want to do this:首先,听起来你想这样做:

Given a list of (name, score) tuples, return a list of names from highest score to lowest score.给定一个 (name, score) 元组列表,返回一个从最高分到最低分的名称列表。

Is that right?那正确吗? I'm going to assume that is the questions.我假设这就是问题所在。

Rather than go in to your code (which, when I run it, only returns ONE of the (name, score) pairs, I'll show you how I'd do it.而不是进入你的代码(当我运行它时,只返回一个(名称,分数)对,我会告诉你我是如何做的。

First in separate steps:首先在单独的步骤中:

recommendations = [
  ('Gloria Pritchett', 2), 
  ('Manny Delgado', 1), 
  ('Cameron Tucker', 1), 
  ('Luke Dunphy', 3)]

rec2 = [(age, name) for name, age in recommendations]
print rec2
rec3 = sorted(rec2, reverse=True)
print rec3
rec4 = [name for age, name in rec3]
print rec4

This will print:这将打印:

[(2, 'Gloria Pritchett'), (1, 'Manny Delgado'), (1, 'Cameron Tucker'), (3, 'Luke Dunphy')]
[(3, 'Luke Dunphy'), (2, 'Gloria Pritchett'), (1, 'Manny Delgado'), (1, 'Cameron Tucker')]
['Luke Dunphy', 'Gloria Pritchett', 'Manny Delgado', 'Cameron Tucker']

Is that what you are looking for?这就是你要找的吗? In my case, "Manny" got sorted before "Cameron" (unlike your intended answer), but if they got the same score, I'm hoping that doesn't matter to you.就我而言,“Manny”在“Cameron”之前排序(与您预期的答案不同),但如果他们的分数相同,我希望这对您来说无关紧要。 (If it does, please clarify your question) (如果是这样,请澄清您的问题)

Old timers like me might call this a variant of the Schwartzian transform .像我这样的老前辈可能会称其为Schwartzian 变换的变体。 (I guess my roots in Perl are showing...) (我猜我在 Perl 中的根源正在显示......)

Anyway, here's how it works:无论如何,这是它的工作原理:

rec2 is a "list comprehension" with score and name swapped. rec2是一个“列表理解”,分数和名称交换了。

rec3 uses the built-in feature of Python to sort tuples, but with reversed=True to get high-to-low sorting. rec3使用 Python 的内置功能对元组进行排序,但使用reversed=True来获得从高到低的排序。

rec4 is another "list comprehension" to drop the score and return the name. rec4是另一种“列表理解”,可以删除分数并返回名称。

If you must, you may put it all together in a single statement:如果必须,您可以将所有内容放在一个语句中:

class_rank = [name for age, name in sorted([(age, name) for name, age in recommendations], reverse=True)]
print class_rank

You are welcome to turn this in to a function, if you'd like.如果您愿意,欢迎您将其转换为函数。

Cheers!干杯!

My variation on the sorted theme.我对排序主题的变体。 I like using itemgetter:我喜欢使用 itemgetter:

>>> 
>>> from operator import itemgetter
>>> name = itemgetter(0)
>>> score = itemgetter(1)
>>> recommendations = [('Gloria Pritchett', 2), ('Manny Delgado', 1), ('Cameron Tucker', 1), ('Luke Dunphy', 3)]
>>> print '\n'.join(map(name, sorted(recommendations, key = score, reverse = True)))
Luke Dunphy
Gloria Pritchett
Manny Delgado
Cameron Tucker
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM