updating dictionaries from/to dictionaries

Brandon your.master at gmail.com
Mon Aug 11 03:27:46 EDT 2008


Hi all,

I am not altogether experienced in Python, but I haven't been able to
find a good example of the syntax that I'm looking for in any tutorial
that I've seen.  Hope somebody can point me in the right direction.

This should be pretty simple:  I have two dictionaries, foo and bar.
I am certain that all keys in bar belong to foo as well, but I also
know that not all keys in foo exist in bar.  All the keys in both foo
and bar are tuples (in the bigram form ('word1', 'word2)).  I have to
prime foo so that each key has a value of 1.  The values for the keys
in bar are variable integers.  All I want to do is run a loop through
foo, match any of its keys that also exist in bar, and add those key's
values in bar to the preexisting value of 1 for the corresponding key
in foo.  So in the end the key,value pairs in foo won't necessarily
be, for example, 'tuple1: 1', but also 'tuple2: 31' if tuple2 had a
value of 30 in bar.

I *think* the get method might work, but I'm not sure that it can work
on two dictionaries the way that I'm getting at.  I thought that
converting the dictionaries to lists might work, but I can't see a way
yet to match the tuple key as x[0][0] in one list for all y in the
other list.  There's just got to be a better way!

Thanks for any help,
Brandon
(trying hard to be Pythonic but isn't there yet)



More information about the Python-list mailing list