英文:
Sorting based on custom sort key
问题
我有一个接收这两个参数的Python函数:
list = [
{"id": "5", "name": "tomato", "value": 3},
{"id": "2", "name": "banana", "value": 2},
{"id": "4", "name": "tomato", "value": 2},
{"id": "1", "name": "banana", "value": 1.5},
{"id": "8", "name": "peas", "value": 1.5},
{"id": "7", "name": "peas", "value": 0.5}
]
"importance"={
"tomato": 2,
"banana": 1,
"peas": 6
}
我基于值预先对列表进行了排序,但现在我想根据重要性在相同值的基础上进行排序,使最终结果如下:
```python
list = [
{"id": "5", "name": "tomato", "value": 3},
{"id": "4", "name": "tomato", "value": 2},
{"id": "2", "name": "banana", "value": 2},
{"id": "8", "name": "peas", "value": 1.5},
{"id": "1", "name": "banana", "value": 1.5},
{"id": "7", "name": "peas", "value": 0.5}
]
如何使用自定义排序函数实现这一点?还是有更好的方法吗?
英文:
I have a python function that receives this 2 arguments:
list = [
{"id": "5", "name": "tomato", "value": 3},
{"id": "2", "name": "banana", "value": 2},
{"id": "4", "name": "tomato", "value": 2},
{"id": "1", "name": "banana", "value": 1.5},
{"id": "8", "name": "peas", "value": 1.5},
{"id": "7", "name": "peas", "value": 0.5}
]
"importance"={
"tomato": 2,
"banana": 1,
"peas": 6
}
I pre ordered the list based on value but now I want to order on the same value floor according to importance so it would look like in the end:
list = [
{"id": "5", "name": "tomato", "value": 3},
{"id": "4", "name": "tomato", "value": 2},
{"id": "2", "name": "banana", "value": 2},
{"id": "8", "name": "peas", "value": 1.5},
{"id": "1", "name": "banana", "value": 1.5},
{"id": "7", "name": "peas", "value": 0.5}
]
How can I do this using custom sort functions? Or is there any better way to do it?
答案1
得分: 1
.sort
和 sorted
函数接受一个 key
参数,该参数可以返回一个数字或数字元组。输出将按照元组的第一个元素,然后是第二个元素等进行排序。这样,你可以根据所有要求进行排序。
l = [
{"id": "5", "name": "tomato", "value": 3},
{"id": "2", "name": "banana", "value": 2},
{"id": "4", "name": "tomato", "value": 2},
{"id": "1", "name": "banana", "value": 1.5},
{"id": "8", "name": "peas", "value": 1.5},
{"id": "7", "name": "peas", "value": 0.5}
]
importance = {
"tomato": 2,
"banana": 1,
"peas": 6
}
print(sorted(l, key=lambda x: (x["value"], importance[x["name"]]), reverse=True))
输出:
[
{'id': '5', 'name': 'tomato', 'value': 3},
{'id': '4', 'name': 'tomato', 'value': 2},
{'id': '2', 'name': 'banana', 'value': 2},
{'id': '8', 'name': 'peas', 'value': 1.5},
{'id': '1', 'name': 'banana', 'value': 1.5},
{'id': '7', 'name': 'peas', 'value': 0.5}
]
注意:使用此代码,不需要预先按值进行排序 - 两个键都会被考虑在内。
英文:
.sort
and sorted
functions accept a key
argument, which can return a number or a tuple of numbers. Output will be sorted by first element of such tuple, then second etc. This way you can sort according to all the requirements you have.
l = [
{"id": "5", "name": "tomato", "value": 3},
{"id": "2", "name": "banana", "value": 2},
{"id": "4", "name": "tomato", "value": 2},
{"id": "1", "name": "banana", "value": 1.5},
{"id": "8", "name": "peas", "value": 1.5},
{"id": "7", "name": "peas", "value": 0.5}
]
importance = {
"tomato": 2,
"banana": 1,
"peas": 6
}
print(sorted(l, key = lambda x: (x["value"], importance[x["name"]]), reverse=True))
Output:
[
{'id': '5', 'name': 'tomato', 'value': 3},
{'id': '4', 'name': 'tomato', 'value': 2},
{'id': '2', 'name': 'banana', 'value': 2},
{'id': '8', 'name': 'peas', 'value': 1.5},
{'id': '1', 'name': 'banana', 'value': 1.5},
{'id': '7', 'name': 'peas', 'value': 0.5}
]
Note: With this code, there is no need to pre-sort according to just value first - both keys are taken into account.
答案2
得分: 1
使用离散函数返回排序键,您可以这样做:
import json
_list = [
{"id": "5", "name": "tomato", "value": 3},
{"id": "2", "name": "banana", "value": 2},
{"id": "4", "name": "tomato", "value": 2},
{"id": "1", "name": "banana", "value": 1.5},
{"id": "8", "name": "peas", "value": 1.5},
{"id": "7", "name": "peas", "value": 0.5}
]
importance = {
"tomato": 2,
"banana": 1,
"peas": 6
}
def key(e):
# 使用负值以避免需要反转排序
return -e['value'], -importance[e['name']]
_list.sort(key=key)
print(json.dumps(_list, indent=2))
输出结果:
[
{
"id": "5",
"name": "tomato",
"value": 3
},
{
"id": "4",
"name": "tomato",
"value": 2
},
{
"id": "2",
"name": "banana",
"value": 2
},
{
"id": "8",
"name": "peas",
"value": 1.5
},
{
"id": "1",
"name": "banana",
"value": 1.5
},
{
"id": "7",
"name": "peas",
"value": 0.5
}
]
英文:
Using a discrete function to return the sort key, you can do this:
import json
_list = [
{"id": "5", "name": "tomato", "value": 3},
{"id": "2", "name": "banana", "value": 2},
{"id": "4", "name": "tomato", "value": 2},
{"id": "1", "name": "banana", "value": 1.5},
{"id": "8", "name": "peas", "value": 1.5},
{"id": "7", "name": "peas", "value": 0.5}
]
importance = {
"tomato": 2,
"banana": 1,
"peas": 6
}
def key(e):
# use -ve values to avoid the need for reversing the sort
return -e['value'], -importance[e['name']]
_list.sort(key=key)
print(json.dumps(_list, indent=2))
Output:
[
{
"id": "5",
"name": "tomato",
"value": 3
},
{
"id": "4",
"name": "tomato",
"value": 2
},
{
"id": "2",
"name": "banana",
"value": 2
},
{
"id": "8",
"name": "peas",
"value": 1.5
},
{
"id": "1",
"name": "banana",
"value": 1.5
},
{
"id": "7",
"name": "peas",
"value": 0.5
}
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论