-2

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?

5
  • You can't assign to strings, drop the " around importance Commented May 25, 2023 at 8:49
  • 1
    Don't use list as a variable name in Python, since it masks the builtin with that name. Choose another name. Commented May 25, 2023 at 8:52
  • It is entirely unclear what you mean by "order on the same value floor according to importance" and how you got the expected result. You should also provide the code you have attempted so we can help find a "better way to do it". Commented May 25, 2023 at 8:54
  • The rationale for the output is unclear. Please explain the logic Commented May 25, 2023 at 8:57
  • @TomKarzes i'm not in my code actually. but thanks for noting :) Commented May 25, 2023 at 9:52

2 Answers 2

1

.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.

Sign up to request clarification or add additional context in comments.

1 Comment

You beat me to it although I prefer a discrete function rather than a lambda on the grounds that it keeps the code tidier and IMHO easier to read
1

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
  }
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.