简体   繁体   English

使用另一个Python列表按键对Python词典进行排序

[英]Sort Python dictionary by it's keys using another Python list

I have a list of words: 我有一个list的话:

['apple', 'zoo', 'chicken', 'needle', 'car', 'computer']

I also have a dictionary with keys and values: 我还有一dictionary包含键和值的dictionary

{'zoo': 42, 'needle': 32, 'computer': 18, 'apple': 39, 'car': 11, 'chicken': 12}

The keys of my dictionary are all from the list of words. dictionary的键全部来自单词list How can I sort the dictionary so that the order of its keys is the same as the order of the words in the list ? 如何对dictionary进行排序,使其键的顺序与list单词的顺序相同? So once sorted, my dictionary should look like this: 因此,一旦排序,我的dictionary应如下所示:

{'apple': 39, 'zoo': 42, 'chicken': 12, 'needle': 32, 'car': 11, 'computer': 18}

Thanks so much! 非常感谢!

For python versions < 3.6, dictionaries do not maintain order, and sorting a dictionary is consequently not possible. 对于版本低于3.6的python,字典不保持顺序,因此无法对字典进行排序。

You may use the collections.OrderedDict to build a new dictionary with the order you want: 您可以使用collections.OrderedDict以所需的顺序构建新词典:

In [269]: from collections import OrderedDict

In [270]: keys = ['apple', 'zoo', 'chicken', 'needle', 'car', 'computer']
     ...: dict_1 = {'zoo': 42, 'needle': 32, 'computer': 18, 'apple': 39, 'car': 11, 'chicken': 12}
     ...: 

In [271]: dict_2 = OrderedDict()

In [272]: for k in keys:
     ...:     dict_2[k] = dict_1[k]
     ...:     

In [273]: dict_2
Out[273]: 
OrderedDict([('apple', 39),
             ('zoo', 42),
             ('chicken', 12),
             ('needle', 32),
             ('car', 11),
             ('computer', 18)])

In Python3.6, a simple dict comprehension suffices: 在Python3.6中,一个简单的dict理解就足够了:

>>> {x : dict_1[x] for x in keys}
{'apple': 39, 'zoo': 42, 'chicken': 12, 'needle': 32, 'car': 11, 'computer': 18}

You can used OrderedDict since regular dictionaries are unordered. 由于常规字典是无序的,因此可以使用OrderedDict。 For your case you could do this: 对于您的情况,您可以这样做:

from collections import OrderedDict
od = OrderedDict()

ll = ['apple', 'zoo', 'chicken', 'needle', 'car', 'computer']
d = {'zoo': 42, 'needle': 32, 'computer': 18, 'apple': 39, 'car': 11, 'chicken': 12} 

for f in ll:
  od[f] = d[f]
#Outputs: OrderedDict([('apple', 39), ('zoo', 42), ('chicken', 12), ('needle', 32), ('car', 11), ('computer', 18)])

Python dict doesn't preserve order by default, you should use collections.OrderedDict . Python dict默认情况下不保留订单,您应该使用collections.OrderedDict The first item you put into OrderedDict is the first item you will get when you enumerate it (eg using for ). 放入OrderedDict的第一项是枚举时将获得的第一项(例如,使用for )。

from collections import OrderedDict

order_list = ['apple', 'zoo', 'chicken', 'needle', 'car', 'computer']
unordered_dict = {'zoo': 42, 'needle': 32, 'computer': 18, 'apple': 39, 'car': 11, 'chicken': 12}
ordered_dict = OrderedDict()
for item in order_list:
    ordered_dict[item] = unordered_dict[item]

for k, v in unordered_dict.items():
    print(k, v)

for k, v in ordered_dict.items():
    print(k, v)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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