In have a data set in the following format:
dataset = {
"one" : { "a" : [ 0, 1, 2 ], "b" : [ 0,10,20 ] },
"two" : { "a" : [ 0, 1 ], "b" : [ 0 ] }
}
I am looking for a quick way to sum the lengths of all the "a" lists (and eventually the same for "b").
So for the above data set I would be looking for the sum to be 5 (as "one"[a] has 3 members and "two"[a] has 2 members, 3+2 is usually 5).
I thought something like this would do the job but I am getting unexpected results (wrong numbers):
print sum( len(e) for d in dataset for e in dataset[d]["a"] )
I thought this would fetch "one" and "two" in turn and for each of these look up the lengths of "a". It would then calculate the sum of all the lengths found. It doesn't, what should I be using?
You're only interested in the values at each level, so just iterate over those:
>>> dataset = {
"one": {"a": [0, 1, 2], "b": [0, 10, 20]},
"two": {"a": [0, 1], "b": [0]}
}
>>> sum(len(lst) for dct in dataset.values() for lst in dct.values())
9
For a specific key in the nested dictionary:
>>> key = 'a'
>>> sum(len(dct[key]) for dct in dataset.values())
5
Or to get counts for multiple keys:
>>> {key: sum(len(dct[key]) for dct in dataset.values()) for key in 'ab'}
{'a': 5, 'b': 4}
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