簡體   English   中英

如何將字典中的鍵與列表中的元素匹配並將這些鍵值存儲到python中的另一個字典中?

[英]How to match keys in a dictionary with the elements in a list and store those key values to another dictionary in 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)

也就是說,不存在的鍵在new_d中的值為0。 但是,有什么方法可以使字典與list1中的單詞具有相同的順序嗎?

像這樣的輸出應該是:

new_d : {'the' : 0, 'of': {'and': 0.6520996477975429, 'in': -0.46306801953487436,'to': 0.8975056783377765}, 'Starbucks' : 0, ......}
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}}

或dict理解:

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 ))

使用dict理解:

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])) }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM