I am trying to get to speed on the use of dictionaries. I spent three hours last night searching the web for examples similar to some of the things I am trying to do. For example, suppose I have two dictionaries (actually I have two lists of dictionaries).
d1={key1:1, key2:2}
d2={key1:1, key2:'A', key4:4}
I want to update d1 so it looks like the following:
d1={key1:1, key2:[2,'A'], key3:3, key4:4}
Ii can't seem to find adequate examples to get me started. I have a fair number of books and I also reviewed them but they all seem to have the same type of examples that I am finding on the web.
Does anyone know of a place or a book that has explicit examples and descriptions of how to use dictionaries?
I think one of the problems I am having is that I am not understanding how references are maintained as I access the dictionary.
I can check to see if the two dictionaries have a common key:
for k in d1.keys():
for k2 in d2.keys():
if k==k2:
print 'true'
but if they do I can't seem to combine the values into a list.
More than a direct answer to this particular example I would appreciate any suggestions about places where there are good examples of using dictionaries.
Try this:
import collections
merged = collections.defaultdict(list)
for k in d1:
merged[k].append( d1[k] )
for k in d2:
merged[k].append( d2[k] )
This may be what you're looking for.
Or possibly this.
import collections
merged = collections.defaultdict(set)
for k in d1:
merged[k].add( d1[k] )
for k in d2:
merged[k].add( d2[k] )
One good place to start is by getting iPython (easy_install ipython
) then poking around with tab completion, ?
and dir
:
In [2]: dir {}
------> dir({})
Out[2]:
['__class__',
...
'keys',
'pop',
'popitem',
'setdefault',
'update',
'values']
In [3]: {}.update?
Type: dict
Base Class: <type 'dict'>
String Form: {}
Namespace: Interactive
Length: 0
Docstring:
dict() -> new empty dictionary.
dict(mapping) -> new dictionary initialized from a mapping object's
(key, value) pairs.
dict(seq) -> new dictionary initialized as if via:
d = {}
for k, v in seq:
d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
(just for example)
Anyway, your problem with checking keys common between the two dictionaries: there are a few things to consider (maybe look at the set
class?), but here's how I'd do it:
common_keys = [k for k in dict1 if k in dict2]
(ie, "each key k
in dict1 if that key is also in dict2")
(note, too, that testing for dictionary membership is an O(1) operation, so this will run in O(|dict1|))
edit: alright, so this doesn't solve the problem of merging the two dicts into one with lists... But Lott's answer is good for that, or you could use the setdefault
method:
new = {}
for (k, v) in dict1.items():
new.setdefault(k, []).append(v)
for (k, v) in dict2.items():
new.setdefault(k, []).append(v)
I believe here is what you want:
>>> d1={'key1':1, 'key2':2}
>>> d2={'key1':1, 'key2':'A', 'key4':4}
>>> d = {}
>>> d.update(d1)
>>> for i in d2:
if i in d and d2[i] != d[i]:
d[i] = [d[i], d2[i]]
else:
d[i] = d2[i]
>>> d
{'key2': [2, 'A'], 'key1': 1, 'key4': 4}
Here are some good links:
http://www.diveintopython.org/getting_to_know_python/dictionaries.html
http://docs.python.org/tutorial/datastructures.html#dictionaries
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With