基于自定义排序键的排序

huangapple go评论132阅读模式
英文:

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

.sortsorted 函数接受一个 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
  }
]

huangapple
  • 本文由 发表于 2023年5月25日 16:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定