Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python通过2个不同类型的键值对json列表进行排序_Python_Json - Fatal编程技术网

Python通过2个不同类型的键值对json列表进行排序

Python通过2个不同类型的键值对json列表进行排序,python,json,Python,Json,我一直在遵循这个答案: 我想根据我的情况对此进行调整: [ {'score': 0.1, 'id': 1, 'flag': true}, {'score': 0.234, 'id': 2, 'flag': false}, {'score': 0.4, 'id': 3, 'flag': false} ] 我需要一个按标志(布尔值)和分数(浮点值)排序的方法,就像在SQL中一样。 在这种情况下,结果将是: [ {'score': 0.1, 'id': 1, '

我一直在遵循这个答案:

我想根据我的情况对此进行调整:

[
    {'score': 0.1, 'id': 1, 'flag': true}, 
    {'score': 0.234, 'id': 2, 'flag': false}, 
    {'score': 0.4, 'id': 3, 'flag': false}
]
我需要一个按标志(布尔值)和分数(浮点值)排序的方法,就像在SQL中一样。 在这种情况下,结果将是:

[
    {'score': 0.1, 'id': 1, 'flag': true}, // first because the flag is true
    {'score': 0.4, 'id': 3, 'flag': false} //second because the score is higher 
    {'score': 0.234, 'id': 2, 'flag': false}, 

]
我正在尝试使用

sorted_result = sorted(json, key=lambda k: (bool(k['flag']), -float(k['score'])))

但是它没有按预期进行排序

您可以使用lambda函数返回一个元组作为排序键。您还可以使用
reverse=True
而不是对值求反,以便按降序排序:False之前为True,值从大到小

j = [
    {'score': 0.1, 'id': 1, 'flag': True},
    {'score': 0.234, 'id': 2, 'flag': False},
    {'score': 0.4, 'id': 3, 'flag': False}
]

sorted(j, key=lambda x: (x.get('flag'), x.get('score')), reverse=True)
# returns:
[{'flag': True, 'id': 1, 'score': 0.1},
 {'flag': False, 'id': 3, 'score': 0.4},
 {'flag': False, 'id': 2, 'score': 0.234}]
如果从字符串加载json,请使用:

import json
j = json.loads('''[
    {"score": 0.1, "id": 1, "flag": true}, 
    {"score": 0.234, "id": 2, "flag": false}, 
    {"score": 0.4, "id": 3, "flag": false}
]''')

只需否定
标志

>>> data = [{'score': 0.1, 'id': 1, 'flag': True}, 
            {'score': 0.234, 'id': 2, 'flag': False}, 
            {'score': 0.4, 'id': 3, 'flag': False}]

>>> sorted(data, key = lambda k: (-k['flag'], -k['score']))
[{'score': 0.1, 'id': 1, 'flag': True}, {'score': 0.4, 'id': 3, 'flag': False}, {'score': 0.234, 'id': 2, 'flag': False}]

您也不需要在这里键入
bool()
float()
,因为类型已经被推断出来了

如果您试图对一个升序为int/float的值进行排序,并且如果存在平局,请对另一个升序为string的值进行排序,这里是:D:

arr = [{'score': 10, 'name': 'Helen'},{'score': 10, 'name': 'Zed'},{'score': 10, 'name': 'Bob'}, {'score': 15, 'name':'Susan'}, {'score': 1, 'name': 'Skippy'}]
arr

[{'score': 10, 'name': 'Helen'},
 {'score': 10, 'name': 'Zed'},
 {'score': 10, 'name': 'Bob'},
 {'score': 15, 'name': 'Susan'},
 {'score': 1, 'name': 'Skippy'}]

sorted(arr, key = lambda k: (k['score'], k['name']))

[{'score': 1, 'name': 'Skippy'},
 {'score': 10, 'name': 'Bob'},
 {'score': 10, 'name': 'Helen'},
 {'score': 10, 'name': 'Zed'},
 {'score': 15, 'name': 'Susan'}]  

你提到的问题似乎给了你所需要的所有信息。但是为了完整性,您应该使用代码生成的实际输出更新您的问题,因为它不包括bool和float标志的组合。排序的结果根本就不存在!您需要提供两个排序键,即
flag,score
这与JSON有什么关系?可能是重复的,这是另一种方法。我觉得这比带着否定词更干净。