简体   繁体   中英

sort a dict based on another dict value

I have two dicts like so:

dict1={'key10': {'fail_pass': 1, 'score': 29.5}, 'key20': {'fail_pass': 0, 'score': 37.25}, 'key30': {'fail_pass': 0, 'score': 25.75}, 'key60': {'fail_pass': 1, 'score': 225.75}, 'key70': {'fail_pass': 1, 'score': 25.25}, 'key170': {'fail_pass': 1, 'score': 0.25}}
dict2={'key10': 1, 'key20': 1, 'key60': 1}

I would like to sort dict2 based on the score of dict1. So, in this case, I want back the dict2 ordered like so:

sorted_dict2={'key60': 1, 'key20': 1, 'key10': 1}

` How do I achieve this?

You can sort your dictonary using sorted function and lambda to access the parameters you want to sort:

dict1={'key10': {'fail_pass': 1, 'score': 29.5}, 'key20': {'fail_pass': 0, 'score': 37.25}, 'key30': {'fail_pass': 0, 'score': 25.75}, 'key60': {'fail_pass': 1, 'score': 225.75}, 'key70': {'fail_pass': 1, 'score': 25.25}, 'key170': {'fail_pass': 1, 'score': 0.25}}
dict2={'key10': 1, 'key20': 1, 'key60': 1}

sortedDict = dict(sorted(dict2.items(), key=lambda x: dict1[x[0]]['score'], reverse=True))

print(sortedDict)

Solution is easy, just pass custom key to built-in sorted() :

dict1 = {'key10': {'fail_pass': 1, 'score': 29.5},
         'key20': {'fail_pass': 0, 'score': 37.25},
         'key30': {'fail_pass': 0, 'score': 25.75},
         'key60': {'fail_pass': 1, 'score': 225.75},
         'key70': {'fail_pass': 1, 'score': 25.25},
         'key170': {'fail_pass': 1, 'score': 0.25}}
dict2 = {'key10': 1, 'key20': 1, 'key60': 1}

sorted_dict2 = dict(sorted(dict2.items(), key=lambda x: dict1[x[0]]['score'], 
                           reverse=True))

If you also need to merge values of both dictionaries, you can sort just keys and then create a dictionary using dict comprehension:

sorted_dict2 = {key: {"value": dict2[key], **dict1[key]}
                for key in sorted(dict2, key=lambda x: dict1[x]['score'],
                                  reverse=True)}

This is more about the explanation of how exactly it gets the values to sort by (explained in code comments):

dict1 = {'key10': {'fail_pass': 1, 'score': 29.5}, 'key20': {'fail_pass': 0, 'score': 37.25}, 'key30': {'fail_pass': 0, 'score': 25.75}, 'key60': {'fail_pass': 1, 'score': 225.75}, 'key70': {'fail_pass': 1, 'score': 25.25}, 'key170': {'fail_pass': 1, 'score': 0.25}}
dict2 = {'key10': 1, 'key20': 1, 'key60': 1}


def sorter(key_value_tuple):
    # `sorted` calls this function by going over each item
    # in `dict2.items()` and passes the item in this case
    # a tuple like this: ('key10', 1)
    
    # so here happens a simple tuple unpacking
    # `key = 'key10'` and `value = 1`
    key, value = key_value_tuple
    # access the dictionary in `dict1`, in this example
    # `value_in_dict1 = {'fail_pass': 1, 'score': 1}`
    value_in_dict1 = dict1[key]
    # then get the score part
    score = value_in_dict1['score']
    # and return the score value `score = 29.5`
    # and so for each item in `dict2.items()`
    return score


sorted_dict = {k: v for k, v in sorted(dict2.items(), key=sorter, reverse=True)}

# also you seemingly don't need a `dict` so you could just use a list
# sorted_values = sorted(dict2, key=lambda x: dict1[x]['score'], reverse=True)
# print(sorted_values)

print(sorted_dict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM