Dictionary question.

Steve Holden steve at holdenweb.com
Thu Apr 21 06:31:15 EDT 2005


Simon Brunning wrote:
> On 21 Apr 2005 02:47:42 -0700, hawkesed <olaamigoquepasa at hotmail.com> wrote:
> 
>>  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.
> 
> 
> Sounds like homework, so I'll just say that yes, a dictionary would be
> ideal for what you are trying to do.
> 
And I will add that you have two different cases to cope with: the first 
time you come across a particular value you have to create a new element 
with a value of one. The second and subsequent times you have to add one 
to an existing element.

regards
  Steve
-- 
Steve Holden        +1 703 861 4237  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/




More information about the Python-list mailing list