python dictionary values sorting

后端 未结 4 2023
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 21:31

I have 2 dictionaries, dict1 and dict2 which contain the same keys, but different values for the keys. What I want to do is for each dictionary, s

4条回答
  •  孤街浪徒
    2020-12-16 21:58

    What version of python are you using? If 2.7, use OrderedDict.

    Per the Python 2.7 docs:

    OrderedDict(sorted(d.items(), key=d.get))
    

    If you're using Python 2.4-2.6 you can still use OrderedDict by installing it from pypi here or if you have setuptools, run

    easy_install ordereddict
    

提交回复
热议问题