Reputation: 312
I have two dictionaries with multiple values:
t={'Musée de Armée':130102845,48.8570374,2.3118779}
While the other s is the sorted Ordered Dictionary of t:
s={'193 Gallery':3359610327,48.8624495,2.3652262}
Both dictionaries have a length of 800 and I would like to merge both dictionaries while keeping their values such that the final result is:
t={'Musée de Armée':130102845,48.8570374,2.3118779,'193 Gallery',3359610327,48.8624495,2.3652262}
This is what I have tried:
s = OrderedDict(sorted(t.items()))
for k,v in t,s:
t[k].append(s.values())
It gives me an error regarding too many values to unpack. So I would like to join sorted and unsorted dictionary into one.
Upvotes: 0
Views: 1071
Reputation: 50180
zip
is indeed a good tool when you have paired elements, as in your case. Since you did not say in your question that there are always exactly three numbers in the values, here is a more general solution that also modifies t
in place, as you intended:
for key, (newkey, newval) in zip (t, s.items()):
t[key].extend([ newkey ] + newval)
This appends the key and values from s to the entry in t. Or you could do it in two steps, it's a matter of taste:
for key, (newkey, newval) in zip (t, s.items()):
t[key].append(newkey)
t[key].extend(newval)
Upvotes: 0
Reputation: 312
To those answering my question and providing guidance, thanks for your help. I had come to a solution the following day after posting this question. I just hadn't had the time to post it.
What worked for me however, was to iterate through both the dictionary items in an array-like manner and the zip method:
zip(*iterables)
Make an iterator that aggregates elements from each of the iterables.
Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.
The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n). This repeats the same iterator n times so that each output tuple has the result of n calls to the iterator. This has the effect of dividing the input into n-length chunks.
zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use itertools.zip_longest() instead.
zip() in conjunction with the * operator can be used to unzip a list
This was what I did:
d = dict()
for (k, v), (k2, v2) in zip(t.items(), s.items()):
d[k] = [v[0], v[1], v[2], k2, v2[0], v2[1], v2[2]]
Upvotes: 0
Reputation: 4412
You can try:
from collections import defaultdict
u = defaultdict(list)
for k, v in in s, t:
u[k].extend(v)
Upvotes: 0
Reputation: 553
In general, you can create a new OrderedDict from two other like that:
my_od = OrderedDict(**s, **t)
Upvotes: 1