简体   繁体   English

在python中对并行列表进行排序-第一项降序,第二项升序

[英]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 . 您可以使用cmpkey作为要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返回从xy的点的比较。 Otherwise it compares the teams from y to x (the opposite order). 否则,它将比较从yx的团队(相反的顺序)。

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.

相关问题 如何按第二项降序和第一项升序对列表进行排序? - How can i sort a list by the second item descending and first one ascending? Python - 对 2 个列表进行排序 - 升序和降序并返回为 2 个列表 - Python - Sorting 2 Lists - both ascending and descending order and returning back as 2 lists 如何在python中按升序排序前半部分,按降序排序后半部分? - How to sort first half in ascending and second half in descending order in python? Java - 按第二项排序包含2个项目的列表列表 - Java - sorting a list of lists with 2 items by a second item 排序字典python值降序,但键升序 - sorting a dictionary python values descending, but keys ascending Python创建列表列表,其中第一项的长度为一,第二项的长度为n? - Python creating list of lists where first item is of length one and the second item is of length n? Python排序词典:键[升序]然后值[降序] - Python sorting dictionaries: Key [Ascending] and then Value [Descending] 在 Python 中对字典进行降序和升序排序 - Sorting dictionary both descending and ascending in Python 一个线性函数,用于根据python中的变化条件以升序,降序对列表的列表进行排序 - One liner function for sorting a string list of lists in ascending, descending order based on varying criteria in python 按值对 Python 字典进行排序; value 是一个包含两项的列表; 对第一个列表项进行常规排序,对第二项进行反向排序 - Sorting Python dictionary by value; value is a two-item list; regular sort on first list item, reverse sort on second item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM