2014-11-14 41 views
1

我有這樣一本字典:如何將字典中的鍵與列表中的元素進行匹配,並將這些鍵值存儲到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

回答

1
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

0

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

相關問題