简体   繁体   中英

How to reverse order of values but not keys in a python dictionnary?

What is the best way to reverse order of values but not keys in a python dictionnary ?

dictionnary = {'a': 3, 'b': 2, 'c': 1}
inversed_values = dictionnary.reverseValues()
print(inversed_values)

This should print out {'a': 1, 'b': 2, 'c': 3}

You can convert values to list then reverse them then create dict like below:

>>> dct = {'a': 3, 'b': 2, 'c': 1}
>>> dict(zip(dct.keys(),list(dct.values())[::-1]))
{'a': 1, 'b': 2, 'c': 3}

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