如何将字典中的键与列表中的元素进行匹配,并将这些键值存储到python中的另一个字典中?

问题描述:

我有这样一本字典:如何将字典中的键与列表中的元素进行匹配,并将这些键值存储到python中的另一个字典中?

{'and': {'in': 0.12241083209661485, 
     'of': 0.6520996477975429, 
     'to': 0.7091938791256235}, 
'in': {'and': 0.12241083209661485, 
     'of': -0.46306801953487436, 
     'to': -0.0654517869126785}, 
'of': {'and': 0.6520996477975429, 
     'in': -0.46306801953487436, 
     'to': 0.8975056783377765}, 
'to': {'and': 0.7091938791256235, 
     'in': -0.0654517869126785, 
     'of': 0.8975056783377765}} 

和列表如下:

list1 = ['the', 'of', 'Starbcks', 'in', 'share', 'for', '%', 'fiscal', '2007', 
     'growth', 'year', 'cents', 'earnings', 'a', 'company', '2,400', 'net', 
     'abot', 'range', 'stores', 'revene', 'sales', 'gidance', '``', "''", 
     'earnings', 'provides', 'wew', 'net'] 

我想通过列表来遍历和检查取其话等于在列表中的键,然后添加这些关键值转换成另一个字典。

从这个例子一样,我想这是我的结果:

new_dict = {'in': {'and': 0.12241083209661485, 
        'of': -0.46306801953487436, 
        'to': -0.0654517869126785}, 
      'of': {'and': 0.6520996477975429, 
        'in': -0.46306801953487436, 
        'to': 0.8975056783377765}} 

我做这样的事情:

for elem in l: 
    temp_dict = dict((key,value) for key,value in similarity_matrix.iteritems() if key == elem) 
print temp_dict 

但我得到{}为我的结果。怎么了,怎么解决?

编辑:

现在,我把这个:

OrderedDict(for k in list1: 
    if k in d: 
     new_d[k] = d[k] 
    else: 
     new_d[k] = 0) 

即,是不是有钥匙,将得到的值0 new_d。但是,有没有办法以与list1中的单词相同的顺序获取字典?

等的输出应该是:

new_d : {'the' : 0, 'of': {'and': 0.6520996477975429, 'in': -0.46306801953487436,'to': 0.8975056783377765}, 'Starbucks' : 0, ......} 
+0

只匹配主键?被发现然后 – Hackaholic 2014-11-14 22:26:49

list1 = ['the', 'of', 'Starbcks', 'in', 'share', 'for', '%', 'fiscal', '2007', 'growth', 'year', 'cents', 'earnings', 'a', 'company', '2,400', 'net', 'abot', 'range', 'stores', 'revene', 'sales', 'gidance', '``', "''", 'earnings', 'provides', 'wew', 'net'] 
d={'and': {'of': 0.6520996477975429, 'in': 0.12241083209661485, 'to': 0.7091938791256235}, 'of': {'and': 0.6520996477975429, 'to': 0.8975056783377765, 'in': -0.46306801953487436}, 'in': {'and': 0.12241083209661485, 'of': -0.46306801953487436, 'to': -0.0654517869126785}, 'to': {'and': 0.7091938791256235, 'of': 0.8975056783377765, 'in': -0.0654517869126785}} 
new_d ={} 
for k in list1: 
    if k in d: 
     new_d[k] = d[k] 
print(new_d) 
{'of': {'and': 0.6520996477975429, 'to': 0.8975056783377765, 'in': -0.46306801953487436}, 'in': {'and': 0.12241083209661485, 'of': -0.46306801953487436, 'to': -0.0654517869126785}} 

或者一个字典理解:

new_d ={ k: d[k] for k in list1 if k in d} 

检查,如果一个关键是在一个字典或一组是O(1)

使用你自己的代码,你可以做以下检查是否在你的清单中的关键:

temp_dict = dict((key,value) for key,value in d.iteritems() if key in set(list1)) 

为了保持项目中添加你需要使用collections.OrderedDict顺序:使用字典理解

from collections import OrderedDict 
new_d = OrderedDict(((k,d[k]) if k in d else (k,0) for k in list1)) 
+0

非常感谢。我编辑了我的问题,有一件事我错过了要求。你可以看看吗?谢谢。 – 2014-11-14 22:58:53

+0

@MarthaPears,添加了一个ordereddict – 2014-11-14 23:17:13

+0

这并没有完全解决我的问题,因为它不会将0添加到不在字典中的单词的值,然后以与列表相同的顺序给它们。 – 2014-11-14 23:30:05

new_dict = { x:y for x,y in my_dict.items() if x in your_list } 

输出:

{'of': {'and': 0.6520996477975429, 'to': 0.8975056783377765, 'in': -0.46306801953487436}, 'in': {'and': 0.12241083209661485, 'of': -0.46306801953487436, 'to': -0.0654517869126785}} 

可以像这样排序:

{ x:y for x,y in sorted(new_dict.items(),key=lambda x:my_list.index(x[0])) } 
+0

我忘了问有一件事,你能再看看我的问题吗?我编辑它。 – 2014-11-14 23:00:55

+0

你在说什么订单? – Hackaholic 2014-11-14 23:03:12

+0

是的,我知道有像OrderDict。但如何使用它? – 2014-11-14 23:04:40