Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the sum of the lengths of a list in a dictionary of dictionaries?

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?

like image 415
user1464409 Avatar asked Feb 11 '15 11:02

user1464409


1 Answers

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}
like image 70
jonrsharpe Avatar answered Sep 29 '22 11:09

jonrsharpe