简体   繁体   English

Python中的列表/元组排序问题

[英]List/Tuple sorting issue in python

I'm a newbie to programming and I'm creating a matchmaking program, to compare personality scores I'm using lists to store, here is my code: 我是编程的新手,我正在创建一个配对计划,以比较我使用列表进行存储的个性得分,这是我的代码:

`ps = int(personality_score)
potential_partners = partners.Partners()
while potential_partners.available():
partner = []
personality_scores = []
a = potential_partners.get_name()
f = int(potential_partners.get_personality_score())
if ps == f:
print("This is your match" + a)
else:
g = abs(int(ps - f))
h = int(g)
personality_scores.append(h)
partner.append(a)
partner_compatability =list(zip(personality_scores, partner))
partner_compatability.sort(key = operator.itemgetter(0))
for sub in partner_compatability:
print(sub)`        

I've looked at multiple questions and answers related to this and none are working for me, my output from the list is this: 我查看了与此相关的多个问题和答案,但没有一个对我有用,列表中的输出是这样的:

`[['Mary Smith', 1]]
[['Juan Lopez', 5]]
[['Leslie Liu', 11]]
[['Tatiana Ivanov', 15]]
[['Andre Leroy', 11]]
[['Sam Augusta', 7]]
[['Adalbert Weber', 1]]`

but should be ordered from lowest score to highest: 但应按从低到高的顺序排序:

`[['Mary Smith', 1]] 
[['Adalbert Weber', 1]]
[['Juan Lopez', 5]]
[['Sam Augusta', 7]]
[['Leslie Liu', 11]]
[['Andre Leroy', 11]]
[['Tatiana Ivanov', 15]]`

(I'm also a newbie programmer and till there is a better answer) you could do what I did: just reverse the order and then sort. (我也是一个新手程序员,直到有一个更好的答案为止),您可以做我所做的事情:只需颠倒顺序然后排序。

lst = [['Mary Smith', 1],
['Juan Lopez', 5],
['Leslie Liu', 11],
['Tatiana Ivanov', 15],
['Andre Leroy', 11],
['Sam Augusta', 7],
['Adalbert Weber', 1]]

Then: 然后:

sorted_lst = [[k,v] for v,k in lst]
sorted_lst.sort()
print(sorted_lst)

You can directly sort the partner_compatability 您可以直接对partner_compatability排序

sorted_list = partner_compatability.sort(key=lambda x: x[1])

I tried like this, as there are no list values provided 我尝试这样,因为没有提供列表值

In [67]: a
Out[67]: [8, 9, 3]
In [68]: b
Out[68]: [5, 6, 3]

In [69]: partner_compatability = list(zip(a, b))

In [70]: partner_compatability
Out[70]: [(8, 5), (9, 6), (3, 3)]

In [71]: partner_compatability.sort(key=lambda x: x[1])
In [72]: partner_compatability
Out[72]: [(3, 3), (8, 5), (9, 6)]

From your comment, 根据您的评论,

partner_compatability = [[(1, 'Mary Smith')],
 [(5, 'Juan Lopez')],
 [(11, 'Leslie Liu')],
 [(15, 'Tatiana Ivanov')],
 [(11, 'Andre Leroy')],
 [(7, 'Sam Augusta')],
 [(1, 'Adalbert Weber')]]

partner_compatability.sort(key=lambda x: x[0][0])

In [71]: partner_compatability
Out[71]:
[[(1, 'Mary Smith')],
 [(1, 'Adalbert Weber')],
 [(5, 'Juan Lopez')],
 [(7, 'Sam Augusta')],
 [(11, 'Leslie Liu')],
 [(11, 'Andre Leroy')],
 [(15, 'Tatiana Ivanov')]]
import operator

data = [
    ['Mary Smith', 1],
    ['Juan Lopez', 5],
    ['Leslie Liu', 11],
    ['Tatiana Ivanov', 15],
    ['Andre Leroy', 11],
    ['Sam Augusta', 7],
    ['Adalbert Weber', 1],
]

data.sort(key = operator.itemgetter(1))
for sub in data:
    print(sub)

prints 版画

['Mary Smith', 1]
['Adalbert Weber', 1]
['Juan Lopez', 5]
['Sam Augusta', 7]
['Leslie Liu', 11]
['Andre Leroy', 11]
['Tatiana Ivanov', 15]

Though not well known, the operator function should be faster than the equivalent lambda. 尽管尚不为人所知,但运算符的功能应比等效的lambda更快。 There is also attrgetter . 还有attrgetter Both can be used when the key should be a tuple of multiple items. 当键应该是多个项目的元组时,都可以使用两者。

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

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