Dictionary question.
Terry Reedy
tjreedy at udel.edu
Thu Apr 21 18:15:22 EDT 2005
"hawkesed" <olaamigoquepasa at hotmail.com> wrote in message
news:1114076862.063504.256970 at g14g2000cwa.googlegroups.com...
> Hi,
> I am semi new to Python. Here is my problem : I have a list of 100
> random integers. I want to be able to construct a histogram out of the
> data. So I want to know how many 70's, 71's, etc. I can't figure out
> how to do this. A dictionary is supposedly can do key value pairs
> right? I want to be able to see if say 75 is in the data structure, and
> what its value is, then increment its value as I go through the list
> finding items.
> I am sure there is a way to do this. Is a dictionary what I should be
> using? Thanks for any help. Hope this makes sense, its getting very
> late here.
The goal of making a histogram implies that there more numbers in rlist
than possible values in range[min(rlist), max(rlist)+1]. I would use a
list.
freq = [0]*(rmax-rmin+1)
for i in randomlist: freq[rmin+i] += 1
You can then further combine bins as needed for a histogram.
If your distribution is sparse instead of compact, a dict would be better,
but 'random integer [in a range]' usually implies compactness.
Terry J. Reedy
More information about the Python-list
mailing list