Multipart - Counting the amount of Values for One key
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Sat Aug 30 01:41:54 CEST 2008
En Fri, 29 Aug 2008 14:41:53 -0300, Ron Brennan <brennan.ron at gmail.com>
escribi�:
> I am trying to find the amount of values there are pertaining to one key.
>
> For example:
>
> - To find the average of the values pertaining to the key.
> - Use the amount of values to calculate a histogram
What is a "multipart"? I know MIME multipart messages but they don't seem
to apply here...
From your other posts I think you're talking about a dictionary mapping
each key to a list of values.
So the values are contained inside a list. It doesn't matter *where* you
store that list, or *how* you get access to it. You have a list of values
- that's all.
Average values (asuming they're all numbers):
def avg(values):
# what to do with an empty list?
return float(sum(values))/len(values) # float() to avoid integer division
There are many libraries that provide histogram support, but using the
bisect module may be enough:
py> from random import randrange
py> from bisect import bisect
py>
py> data = [randrange(100)+randrange(100) for i in range(1000)]
py> bins = range(0, 200, 20) # 0, 20, 40..180
py> counts = [0]*len(bins)
py> for point in data:
... bin = bisect(bins, point)-1
... counts[bin] += 1
...
py> for bin, count in zip(bins,counts):
... print bin, count
...
0 16
20 71
40 107
60 140
80 170
100 180
120 148
140 110
160 43
180 15
> Also, how do reference a specific value for a key in a multipart?
It's just a list: the third value is values[2], the last one is
values[-1]...
--
Gabriel Genellina
More information about the Python-list
mailing list