如何将字典中的键和列表中的元素相匹配,并将这些键值存储到python中的另一个字典中?
我有一本这样的字典:如何将字典中的键和列表中的元素相匹配,并将这些键值存储到python中的另一个字典中?,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.6520996
{'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']
for elem in l:
temp_dict = dict((key,value) for key,value in similarity_matrix.iteritems() if key == elem)
print temp_dict
{ x:y for x,y in sorted(new_dict.items(),key=lambda x:my_list.index(x[0])) }
下面是一个列表:
{'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']
for elem in l:
temp_dict = dict((key,value) for key,value in similarity_matrix.iteritems() if key == elem)
print temp_dict
{ x:y for x,y in sorted(new_dict.items(),key=lambda x:my_list.index(x[0])) }
我想遍历列表并检查列表中与键相等的单词,然后将这些键值添加到另一个字典中
如本例所示,我希望得到以下结果:
new_dict = {'in': {'and': 0.12241083209661485,
'of': -0.46306801953487436,
'to': -0.0654517869126785},
'of': {'and': 0.6520996477975429,
'in': -0.46306801953487436,
'to': 0.8975056783377765}}
我正在做这样的事情:
{'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']
for elem in l:
temp_dict = dict((key,value) for key,value in similarity_matrix.iteritems() if key == elem)
print temp_dict
{ x:y for x,y in sorted(new_dict.items(),key=lambda x:my_list.index(x[0])) }
但是我得到了{}
作为我的结果。出了什么问题以及如何解决
编辑:
现在,我拿了这个:
OrderedDict(for k in list1:
if k in d:
new_d[k] = d[k]
else:
new_d[k] = 0)
i、 例如,不存在的键将在new_d中获得值0。但是,有没有办法让字典的顺序与列表1中的单词相同
类似的输出应该是:
new_d : {'the' : 0, 'of': {'and': 0.6520996477975429, 'in': -0.46306801953487436,'to': 0.8975056783377765}, 'Starbucks' : 0, ......}
或听写理解:
new_d ={ k: d[k] for k in list1 if k in d}
检查钥匙是否在dict或集合中是O(1)
使用您自己的代码,您可以执行以下操作,检查密钥是否在您的列表中:
temp_dict = dict((key,value) for key,value in d.iteritems() if key in set(list1))
要保持添加项目的顺序,您需要使用:
使用听写理解:
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}}
您可以这样排序:
{'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']
for elem in l:
temp_dict = dict((key,value) for key,value in similarity_matrix.iteritems() if key == elem)
print temp_dict
{ x:y for x,y in sorted(new_dict.items(),key=lambda x:my_list.index(x[0])) }
只匹配主键???非常感谢你。我编辑了我的问题,有一件事我没问。你能看一下吗?谢谢。@MarthaPears,添加了一个OrderedDict,但这并没有完全解决我的问题,因为它不会给字典中没有的单词的值添加0,然后按照与列表相同的顺序给出它们。只需将编辑中的代码替换为
OrderedDict
,使成为新的OrderedDict
,我在理解中编辑了答案,你的意思是:OrderedDict(对于列表1中的k:如果k in d:new\u d[k]=d[k]其他:new\u d[k]=0)?我忘了问一件事,你能再看看我的问题吗?我编辑过。是的,我知道有点像OrderDict。但是如何使用它呢?我没有完全解决我的问题。它给了我和以前一样的结果。检查我编辑的问题。