Home > database >  find average and sum of unique values count for dictionary values
find average and sum of unique values count for dictionary values

Time:04-01

I have an input dictionary like d={'a':['ar','br','cb','dr'],'bn':['ar','bm','cm','dm']}, there is a need to find out average for values count within the dictionary. Also, I want to find out all the unique values within the dictionary. I need a generalized solution. The output I expect is for average the result should be 4. For the sum of all unique values count, I expect 7 as output.

d={'a':['ar','br','cb','dr'],'bn':['ar','bm','cm','dm']} Output: for average output should be 4. for unique values count output should be 7.

CodePudding user response:

For the mean:

sum(len(v) for v in d.values())/len(d)

For the uniques:

len( set(item for v in d.values() for item in v))
  • Related