问题
So I have an array of tuples something like this
query_results = [("foo", "bar"), ("foo", "qux"), ("baz", "foo")]
I would like to achieve something like:
{
"foo": ["bar", "qux"],
"baz": ["foo"]
}
So I have tried using this
from itertools import groupby
grouped_results = {}
for key, y in groupby(query_results, lambda x: x[0]):
grouped_results[key] = [y[1] for u in list(y)]
The issue I have is although the number of keys are correct, the number of values in each array is dramatically lower than it should be. Can anyone explain why this happens and what I should be doing?
回答1:
You better use a defaultdict
for this:
from collections import defaultdict
result = defaultdict(list)
for k,v in query_results:
result[k].append(v)
Which yields:
>>> result
defaultdict(<class 'list'>, {'baz': ['foo'], 'foo': ['bar', 'qux']})
If you wish to turn it into a vanilla dictionary again, you can - after the for
loop - use:
result = dict(result)
this then results in:
>>> dict(result)
{'baz': ['foo'], 'foo': ['bar', 'qux']}
A defaultdict
is constructed with a factory, here list
. In case the key cannot be found in the dictionary, the factory is called (list()
constructs a new empty list). The result is then associated with the key.
So for each key k
that is not yet in the dictionary, we will construct a new list first. We then call .append(v)
on that list to append values to it.
回答2:
Well why not use a simple for
loop?
grouped_results = {}
for key, value in query_results:
grouped_results.setdefault(key, []).append(value)
Output:
{'foo': ['bar', 'qux'], 'baz': ['foo']}
回答3:
How about using a defaultdict
?
d = defaultdict(list)
for pair in query_results:
d[pair[0]].append(pair[1])
来源:https://stackoverflow.com/questions/45490667/python-array-of-tuples-group-by-first-store-second