Reputation: 149
I have a dictionary like this:
{'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}}
and a list like this:
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']
I want to traverse through the list and check whichever words equals the keys in the list, and then add those key values into another dictionary.
Like from this example, I want this as my result:
new_dict = {'in': {'and': 0.12241083209661485,
'of': -0.46306801953487436,
'to': -0.0654517869126785},
'of': {'and': 0.6520996477975429,
'in': -0.46306801953487436,
'to': 0.8975056783377765}}
I am doing something like this:
for elem in l:
temp_dict = dict((key,value) for key,value in similarity_matrix.iteritems() if key == elem)
print temp_dict
But I getting {}
as my result. What's wrong and how to fix it?
EDIT:
Now, I took this:
OrderedDict(for k in list1:
if k in d:
new_d[k] = d[k]
else:
new_d[k] = 0)
i.e., the keys that are not there, will get values 0 in new_d. But, is there any way to get the dictionary in the same order as there were words in list1?
Like the output should be:
new_d : {'the' : 0, 'of': {'and': 0.6520996477975429, 'in': -0.46306801953487436,'to': 0.8975056783377765}, 'Starbucks' : 0, ......}
Upvotes: 1
Views: 179
Reputation: 180391
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}}
Or a dict comprehension:
new_d ={ k: d[k] for k in list1 if k in d}
checking if a key is in a dict or a set is O(1)
Using your own code you could do the following which checks if the key is in your list:
temp_dict = dict((key,value) for key,value in d.iteritems() if key in set(list1))
To keep the order the items were added you need to use collections.OrderedDict:
from collections import OrderedDict
new_d = OrderedDict(((k,d[k]) if k in d else (k,0) for k in list1 ))
Upvotes: 1
Reputation: 19733
using dict comprehension:
new_dict = { x:y for x,y in my_dict.items() if x in your_list }
output:
{'of': {'and': 0.6520996477975429, 'to': 0.8975056783377765, 'in': -0.46306801953487436}, 'in': {'and': 0.12241083209661485, 'of': -0.46306801953487436, 'to': -0.0654517869126785}}
you can sort like this:
{ x:y for x,y in sorted(new_dict.items(),key=lambda x:my_list.index(x[0])) }
Upvotes: 0