I have a dictionary with a list of values of varying numbers of items. I want to compare the values of this dictionary (dict1) to the keys of another (dict2), and if they match print the key and value of the matching component of dict1, along with the value of dict2. Both of the dictionaries are quite large, and currently this is taking way too long as you could have guessed from this basic script.
dict1 = {boys:[tom,jon],girls:[suzy]}
dict2 = {suzy:paper-stapler-extraordinaire,jon:paper-shredderoligist,tom:garbage-specialist}
output:
boys \t tom \t garbage-specialist
boys \t jon \t paper-shredderoligist etc.....
for k,v in dict2.items():
for key,value in dict1.items():
if k in value[0]:
print str(key)+"\t"+str(value[0])+"\t"+v
if len(value)>1:
if k in value[1]:
print str(key)+"\t"+str(value[0])+"\t"+v
Could someone suggest a more memory efficient method? Perhaps a list comprehension? This has not been working... a = [k for k in dict2 if k in dict]
for dict1_key, dict1_values in dict1.iteritems():
for dict1_value in dict1_values:
try:
dict2_value = dict2[dict1_value]
print str(dict1_key) + '\t' + str(dict1_value) + '\t' + str(dict2_value)
except KeyError:
pass
That combines a few techniques to speed it up and use less memory. iteritems
uses less memory (as others have mentioned). By using try
and then using dict2[dict1_value]
, you can ensure that dict2
is only searched once, and then with the hash algorithm, which should be much faster than iterating through all the elements. For all the cases where dict2
does not have dict1_value
in it, the first statement under try fails, causing the catch
block to harmlessly pass
.
Are you looking for something along the lines of:
[(k,i,dict2[i]) for k,v in dict1.items() for i in v if i in set(dict2.keys())]
which returns the key,value of dict1 and value of dic2 for every value in dict1 that is a key in dict2. This can be edited to return a string etc...
This outputs:
[('boys', 'tom', 'garbage-specialist'),
('boys', 'jon', 'paper-shredderoligist'),
('girls', 'suzy', 'paper-stapler-extraordinaire')]
The set() on dict2.keys() is used to make the key look-up faster.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.