How to do an 'inner join' with dictionaries

T

Tim Chase

Let's say I have two dictionaries:
dict1 is 1:23, 2:76, 4:56
dict2 is 23:A, 76:B, 56:C

How do I get a dictionary that is
1:A, 2:B, 4:C
>>> d1={1:23,2:76,4:56}
>>> d2={23:"a", 76:"b", 56:"c"}
>>> result = dict([(d1k,d2[d1v]) for (d1k, d1v) in d1.items()])
>>> result
{1: 'a', 2: 'b', 4: 'c'}



If you don't know that all the values in d1 will be in d2,
>>> d1[5]=444
>>> d2[333]='qqq'

you can put in a "if" check like
>>> result = dict([(d1k,d2[d1v]) for (d1k, d1v) in
d1.items() if d1v in d2])
{1: 'a', 2: 'b', 4: 'c'}

(using "d1v" for "dictionary1's value" and "d1k" for
"dictionary1's key")

HTH,

-tkc
 
C

cyborg4

Let's say I have two dictionaries:
dict1 is 1:23, 2:76, 4:56
dict2 is 23:A, 76:B, 56:C

How do I get a dictionary that is
1:A, 2:B, 4:C
 
K

Kent Johnson

Let's say I have two dictionaries:
dict1 is 1:23, 2:76, 4:56
dict2 is 23:A, 76:B, 56:C

How do I get a dictionary that is
1:A, 2:B, 4:C
>>> dict1 = {1:23, 2:76, 4:56}
>>> dict2 = {23:'A', 76:'B', 56:'C'}
>>> dict((key, dict2[value]) for key, value in dict1.iteritems())
{1: 'A', 2: 'B', 4: 'C'}

Kent
 
A

Antoon Pardon

Op 2006-02-27 said:
Let's say I have two dictionaries:
dict1 is 1:23, 2:76, 4:56
dict2 is 23:A, 76:B, 56:C

How do I get a dictionary that is
1:A, 2:B, 4:C

Well how about the straight forward:

dict3 = {}
for key, value in dict1.iteritems():
try:
dict3[key] = dict2[value]
except KeyError:
pass

Or otherwise with a genexp

dict3 = dict((key, dict2[value]) for (key, value) in dict1.iteritems() if value in dict2)
 
C

Claudio Grondi

Let's say I have two dictionaries:
dict1 is 1:23, 2:76, 4:56
dict2 is 23:A, 76:B, 56:C

How do I get a dictionary that is
1:A, 2:B, 4:C
Just copy/paste the following source code to a file and run it:
<code>
sourceCodeToExecute = """
dict1 = { 1:23, 2:76, 4:56 }
dict2 = { 23:'A', 76:'B', 56:'C' }
# How do I get a dictionary that is
dict3 = { 1:'A', 2:'B', 4:'C' }

dict4 = {}
for key in dict1.keys():
dict4[key] = dict2[dict1[key]]
#:for

print 'dict3 == dict4 is', dict3 == dict4
"""

print
print ' execution of following source code: '
print
print '"""',
print sourceCodeToExecute
print '"""'
print
print ' results in output of: '
print
exec(sourceCodeToExecute)
</code>

The core idea of an inner join:
dict2[dict1[key]]


Hope this helps inspite of the fact there is some more complicated code
involved, which I provided to show the beauty of Python from the point
of view of my own style of writing 'tutorial-like' code which explains
itself by its output.

Claudio
 
M

Michael J. Fromberger

Let's say I have two dictionaries:
dict1 is 1:23, 2:76, 4:56
dict2 is 23:A, 76:B, 56:C

How do I get a dictionary that is
1:A, 2:B, 4:C

The following should work:

j = dict((k, dict2[dict1[k]]) for k in dict1 if dict1[k] in dict2)

Cheers,
-M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top