Sorting a dictionary with values as tuple

mars

I have a dictionary as follows and I want to sort this based on the last element of the tuple in a non-increasing manner. However, if two or more of the last elements are the same, then they should be sorted based on the second element.

    capacity= {
    'one': (57000, 3100, 0.12903225806451613),
 'two': (52911, 2400, 0.125),
 'three': (48200, 2400, 0.125),
 'four': (48200, 2480, 0.125),
 'five': (11464, 1200, 0.20833333333333334),
 'six': (44000, 1172, 0.21331058020477817),
 'seven': (42000, 1172, 0.21331058020477817)}

After sorting by the third element of the tuple I get the following results.

sorted_capacity={k: (v1,v2,v3) for k, (v1,v2,v3) in sorted(capacity.items(), key=lambda x: x[1][2],reverse=True)}



sorted_capacity

{'six': (44000, 1172, 0.21331058020477817),
 'seven': (42000, 1172, 0.21331058020477817),
 'five': (11464, 1200, 0.20833333333333334),
 'one': (57000, 3100, 0.12903225806451613),
 'two': (52911, 2400, 0.125),
 'three': (48200, 2400, 0.125),
 'four': (48200, 2480, 0.125)}

However, as I mentioned before, I need to have 'four': (48200, 2480, 0.125)} before 'two': (52911, 2400, 0.125), because 2480 is higher than 2400. Basically my output should be like this:

sorted_capacity

    {'six': (44000, 1172, 0.21331058020477817),
     'seven': (42000, 1172, 0.21331058020477817),
     'five': (11464, 1200, 0.20833333333333334),
     'one': (57000, 3100, 0.12903225806451613),
     'four': (48200, 2480, 0.125),
     'two': (52911, 2400, 0.125),
     'three': (48200, 2400, 0.125)
     }

Can anyone help me figure out how to achieve this?

Barmar

Use

key = lambda x: (x[1][-1], x[1][1])

This compares tuples of the last and second elements in each tuple. Since tuples are ordered lexicographically, this produces the ordering you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sorting on multiple keys from heterogenous tuple in values of a python dictionary

From Dev

Sorting dictionary with tuple keys by specified element of tuple

From Dev

Dictionary with multiple values to tuple

From Dev

Sorting dictionary list values

From Dev

Sorting the dictionary by values

From Dev

sorting a dictionary by mean of their values

From Dev

Sorting a Dictionary by Key and then Value (list or tuple?)

From Dev

Construct tuple from dictionary values

From Dev

Julia : construct Dictionary with tuple values

From Dev

Extracting the values from dictionary with tuple

From Dev

Sorting dictionary keys for the equal values

From Dev

Sorting Dictionary on two values in swift

From Java

python sorting dictionary by length of values

From Java

Sorting a dictionary with multiple sized values

From Dev

sorting a list by values of a dictionary python

From Dev

Sorting a dictionary by value with tuples as values

From Dev

Sorting a dictionary and then replace the values by ranking

From Dev

sorting dictionary values asc in python

From Dev

Adding multiple values to a dictionary and then sorting it

From Dev

Add tuple values as values for key in dictionary

From Dev

Change all values of dictionary with tuple keys in python

From Dev

C# instantiate dictionary with tuple with values

From Dev

NSCoding swift dictionary with swift tuple as values

From Dev

Sort a dictionary based on 'parent' tuple values

From Dev

Unpacking Dictionary tuple values in one go

From Dev

Access key values of dictionary with tuple as key

From Dev

How to sort a dictionary based on values with tuple

From Dev

How to create a tuple of certain values in a dictionary comprehension?

From Dev

Convert a list of tuples to a dictionary, based on tuple values

Related Related

  1. 1

    Sorting on multiple keys from heterogenous tuple in values of a python dictionary

  2. 2

    Sorting dictionary with tuple keys by specified element of tuple

  3. 3

    Dictionary with multiple values to tuple

  4. 4

    Sorting dictionary list values

  5. 5

    Sorting the dictionary by values

  6. 6

    sorting a dictionary by mean of their values

  7. 7

    Sorting a Dictionary by Key and then Value (list or tuple?)

  8. 8

    Construct tuple from dictionary values

  9. 9

    Julia : construct Dictionary with tuple values

  10. 10

    Extracting the values from dictionary with tuple

  11. 11

    Sorting dictionary keys for the equal values

  12. 12

    Sorting Dictionary on two values in swift

  13. 13

    python sorting dictionary by length of values

  14. 14

    Sorting a dictionary with multiple sized values

  15. 15

    sorting a list by values of a dictionary python

  16. 16

    Sorting a dictionary by value with tuples as values

  17. 17

    Sorting a dictionary and then replace the values by ranking

  18. 18

    sorting dictionary values asc in python

  19. 19

    Adding multiple values to a dictionary and then sorting it

  20. 20

    Add tuple values as values for key in dictionary

  21. 21

    Change all values of dictionary with tuple keys in python

  22. 22

    C# instantiate dictionary with tuple with values

  23. 23

    NSCoding swift dictionary with swift tuple as values

  24. 24

    Sort a dictionary based on 'parent' tuple values

  25. 25

    Unpacking Dictionary tuple values in one go

  26. 26

    Access key values of dictionary with tuple as key

  27. 27

    How to sort a dictionary based on values with tuple

  28. 28

    How to create a tuple of certain values in a dictionary comprehension?

  29. 29

    Convert a list of tuples to a dictionary, based on tuple values

HotTag

Archive