简体   繁体   English

python,排序不起作用(随机结果)

[英]python, sort doesn't work (random result)

I have the following python code:我有以下 python 代码:

def calcCompetitionsResults(competitors_in_competitions): #competitors_in_competitions is list of dictionaries

    competitions_champs = []
    competitions = {elem['competition name']: elem['competition type'] for elem in competitors_in_competitions}
    for compitition in competitions:
        list = [elem for elem in competitors_in_competitions if elem['competition name'] == compitition]
        print(list)
        list = sorted(list, key=itemgetter('result'), reverse=True)
        print(list)

output: (Before) output:(之前)

[{'competition name': 'high_jump', 'competitor id': '2', 'competition type': 'untimed', 'result': '101', 'competitor country': 'USA'}, {'competition name': 'high_jump', 'competitor id': '1', 'competition type': 'untimed', 'result': '96', 'competitor country': 'Canada'}, {'competition name': 'high_jump', 'competitor id': '1', 'competition type': 'untimed', 'result': '100', 'competitor country': 'Canada'}, {'competition name': 'high_jump', 'competitor id': '3', 'competition type': 'untimed', 'result': '90', 'competitor country': 'China'}] [{'比赛名称': 'high_jump', 'competitor id': '2', '比赛类型': 'untimed', '结果': '101', '参赛国家': 'USA'}, {'比赛name': 'high_jump', 'competitor id': '1', 'competition type': 'untimed', 'result': '96', 'competitor country': '加拿大'}, {'competition name': ' high_jump', 'competitor id': '1', 'competition type': 'untimed', 'result': '100', 'competitor country': 'Canada'}, {'competition name': 'high_jump', '竞争对手id':'3','比赛类型':'untimed','结果':'90','竞争对手国家':'中国'}]

(After): (后):

[{'competition name': 'high_jump', 'competitor id': '1', 'competition type': 'untimed', 'result': '96', 'competitor country': 'Canada'}, {'competition name': 'high_jump', 'competitor id': '3', 'competition type': 'untimed', 'result': '90', 'competitor country': 'China'}, {'competition name': 'high_jump', 'competitor id': '2', 'competition type': 'untimed', 'result': '101', 'competitor country': 'USA'}, {'competition name': 'high_jump', 'competitor id': '1', 'competition type': 'untimed', 'result': '100', 'competitor country': 'Canada'}] [{'比赛名称': 'high_jump', 'competitor id': '1', '比赛类型': 'untimed', '结果': '96', '参赛国': '加拿大'}, {'比赛name': 'high_jump', 'competitor id': '3', 'competition type': 'untimed', 'result': '90', 'competitor country': 'China'}, {'competition name': ' high_jump', 'competitor id': '2', 'competition type': 'untimed', 'result': '101', 'competitor country': 'USA'}, {'competition name': 'high_jump', '竞争对手id':'1','比赛类型':'untimed','结果':'100','竞争对手国家':'加拿大'}]

as you can see it wasn't sorted at all according to result field...如您所见,它根本没有根据结果字段进行排序...

The result are string so you should convert it to int first, also please don't use list as variable name as python already used it, here I replaced list with l : result是字符串,所以你应该先将其转换为int ,也请不要使用list作为变量名,因为 python 已经使用了它,这里我将list替换为l

l = sorted(l, key=lambda x: int(x['result']), reverse=True)

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

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