python - How to map identical items in two dictionaries and produce a list of values of their corresponding keys -


my goal map small dictionary large 1 , return list of values in large dictionary bears corresponding keys small dictionary.

x={'red':0.25, 'yellow':0.05, 'pink':0.35, 'brown':0.22, 'blue':0.13} y={'red':2, 'blue':3, 'yellow':1} 

my code keeps giving me list of full values of large dictionary.

for b in x.keys():     if b in y.keys():         k=y.values() print k  output: [0.35, 0.22, 0.13, 0.05, 0.25]  desired output: [0.25,0.13,0.05] 

any suggestions? thanks.

something this? assume order not matter since these dictionaries. also, assuming since seem iterating on x.keys(), then, there may values in y not present in x , don't want them mapped.

>>> x={'red':0.25, 'yellow':0.05, 'pink':0.35, 'brown':0.22, 'blue':0.13} >>> y={'red':2, 'blue':3, 'yellow':1} >>> [val elem, val in x.items() if elem in y] [0.13, 0.05, 0.25] 

if there no values in y not in x, iterate on y dictionary.

>>> [x[key] key in y] [0.13, 0.05, 0.25] 

p.s. - problem in code everytime find match, assign whole list of y.values() k, hence ending complete list of values. modify code this

>>> k = [] >>> b in x.keys():     if b in y.keys():         k.append(x[b])   >>> k [0.13, 0.05, 0.25] 

although, iterating on dictionary gives similar result, follows

>>> b in x:     if b in y:         k.append(x[b])   >>> k [0.13, 0.05, 0.25] 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -