[英]Sorting parallel lists in python - first item descending, second item ascending
I have two lists - one for a team name and the second for points 我有两个清单-一个代表球队名称,第二个代表积分
I wanted to sort them based on the points without losing track of the corresponding team. 我想根据分数对它们进行排序,而又不会失去对相应团队的跟踪。 I found something on SO that works great for doing this - and it sorts teams by points in descending order (most to least).
我在SO上找到了一些非常适合执行此操作的内容-并按得分降序(从最小到最大)对团队进行了排序。
There doesn't appear to be a sort on the second list. 第二个列表上似乎没有排序。
EDIT - Original Data
编辑-原始数据
Teams Pts
D Clark 0
D Dupuis 2
T Leach 2
J Schutz 0
C Hearder 2
R Pagani 0
M Cameron 2
B Hunter 0
This is what I'm using: 这就是我正在使用的:
pts, teams = zip(*sorted(zip(pts, teams), reverse=True))
i = 1
for team, num_pts in zip(teams, pts):
standings = str(i) + '. ' + team + " (" + num_pts + ")"
print standings
i += 1
And it results in this: 结果是:
1. T Leach (2)
2. M Cameron (2)
3. D Dupuis (2)
4. C Hearder (2)
5. R Pagani (0)
6. J Schutz (0)
7. D Clark (0)
8. B Hunter (0)
What I'm trying to do is to add an additional sort on team name - so if there's a tie, then it will sort by names in ascending order, 我想做的是在团队名称上添加其他排序-因此,如果有平局,它将按名称升序排序,
Like This: 像这样:
1. C Hearder (2)
2. D Dupuis (2)
3. M Cameron (2)
4. T Leach (2)
5. B Hunter (0)
6. D Clark (0)
7. J Schutz (0)
8. R Pagani (0)
You have two options here. 您在这里有两个选择。 You could use
cmp
or key
as a parameter to sorted
. 您可以使用
cmp
或key
作为要sorted
的参数。
With a cmp
you're defining a function which takes two arguments, the two values being compared. 使用
cmp
您正在定义一个接受两个参数的函数,这两个值将进行比较。 If the first argument is smaller than the second, return a negative number. 如果第一个参数小于第二个参数,则返回负数。 If it is larger than the second, return a positive number.
如果大于第二个,则返回一个正数。 If they are equal, return 0.
如果它们相等,则返回0。
First example using cmp
: 使用
cmp
第一个示例:
pts = ['2', '2', '2', '2', '2', '0', '0', '0', '0']
teams = ['T Leach', 'M Cameron', 'D Dupuis', 'C Hearder', 'R Pagani',
'J Schutz', 'D Clark', 'B Hunter']
def custom_cmp(x,y):
return cmp(x[0], y[0]) or cmp(y[1], x[1])
pts, teams = zip(*sorted(zip(pts, teams), cmp=custom_cmp, reverse=True))
The python or
operator will return the left hand side if it evaluates to True
(not 0
as described above), otherwise it will return the right hand side. 如果python
or
运算符的结果为True
(如上所述,不是0
,则返回左侧),否则它将返回右侧。 Therefore this custom_cmp
returns the comparison of the points from x
to y
if the points are not equivalent. 因此,如果这些点不相等,则此
custom_cmp
返回从x
到y
的点的比较。 Otherwise it compares the teams from y
to x
(the opposite order). 否则,它将比较从
y
到x
的团队(相反的顺序)。
With key
on the other hand you are just considering how to view an element individually in order that python will naturally understand how to sort it how you want. 另一方面,使用
key
时,您只是在考虑如何单独查看元素,以便python自然了解如何按需要对其进行排序。 We know the first item is being compared properly, so now we just need to flip the second. 我们知道第一个项目已被正确比较,所以现在我们只需要翻转第二个项目即可。 Numbers would be easy, we could just take the negative.
数字很容易,我们可以取负数。 But how do you flip a string?
但是,如何翻转字符串?
One approach is to take the ord
(which should be an ascii value integer for your strings) of each character in the string. 一种方法是获取字符串中每个字符的
ord
(对于您的字符串,应为ascii值整数)。 Then you can take the negative of these values and package them into a tuple
. 然后,您可以取这些值的负数并将它们打包为一个
tuple
。 This is what one of these would look like: 这是其中之一:
>>> tuple(-ord(c) for c in 'C Hearder')
(-67, -32, -72, -101, -97, -114, -100, -101, -114)
Second example using key
: 使用
key
第二个示例:
def custom_key(pair):
pts, team = pair
return (pts, tuple(-ord(c) for c in team))
pts, teams = zip(*sorted(zip(pts, teams), key=custom_key, reverse=True))
Restrospective edit: 回顾性编辑:
If we're defining a custom key
we may as well invert the key
itself and remove the reverse
argument: 如果我们要定义一个自定义
key
我们也可以将key
本身反转并删除reverse
参数:
def custom_key(pair):
pts, team = pair
return (-int(pts), team)
pts, teams = zip(*sorted(zip(pts, teams), key=custom_key))
You can specify how sorted
will sort by passing in the key
kwarg. 您可以通过传入
key
kwarg来指定sorted
方式。 One handy way to use key
is with itemgetter
from the operator
module. 一种便捷的使用
key
是通过operator
模块中的itemgetter
。
For example, if you have a list of tuples (such as by zipping two lists) and you want to sort by the second item, then the first, the key would be itemgetter(1,0)
so you might do something like this: 例如,如果您有一个元组列表(例如通过压缩两个列表),并且想要按第二项排序,那么第一项的键将是
itemgetter(1,0)
因此您可以执行以下操作:
from operator import itemgetter
teams_and_points = zip(teams, pts)
for team, points in sorted(points_and_teams, key=itemgetter(1, 0)):
print(team, points)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.