I am trying to perform a calculation on multiple values in the value portion of a list of key:value pairs.
So I have something like:
[(‘apples’, [‘254’, ‘234’, ’23’, ’33’]), (‘bananas’, [‘732′, ’28’]), (‘squash’, [‘3’])]
I’m trying to create a list of y-values that are the averages of those integers above.
What I’m trying to write is the following pseudocode
if item[1] contains 0 elements:
ys.append(item[1])
else if item[1] contains > 0 elements:
add up elements, divide by number of elements
ys.append average number
What I’m not sure how to do is how to access all of the values that might exist in each key’s value set.
Where
pairsis your list of pairs:will give you a list of average values.
If your numbers are strings, as in your example, replace
sum(values)above withsum([int(i) for i in values]).EDIT: And if you rather want a dictionary then a list of averages: