Dictionary question.

H

hawkesed

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.
Regards all.
Ed
 
S

Simon Brunning

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.
 
S

Steve Holden

Simon said:
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
 
T

Terry Reedy

hawkesed said:
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
 
H

hawkesed

Here is an example of the input list:
[101, 66, 75, 107, 108, 101, 106, 98, 111, 88, 119, 93, 115, 95, 114,
95, 118, 109, 85, 75, 88, 97, 53, 78, 98, 91, 115, 77, 107, 153, 108,
101]

Here is the code I am working on now:.... if adict.has_key(num):
.... x = adict.get(num)
.... x = x + 1
// update the value here
else
// add the new value here.
Its these two commented out lines that I am working on. For the record,
I am not in a python class. I am trying to learn python. For one thing
I thought it looks like it would be a good tool to work with math and
statistics stuff. Thanks for the help.
Have a great day.
Ed
 
H

hawkesed

Steve,
thanks for the input. That is actually what I am trying to do, but I
don't know the syntax for this in python. For example here is a list I
want to work with as input:
[101, 66, 75, 107, 108, 101, 106, 98, 111, 88, 119, 93, 115, 95, 114,
95, 118, 109, 85, 75, 88, 97, 53, 78, 98, 91, 115, 77, 107, 153, 108]
Here is as far as I have gotten coding:.... if adict.has_key(num):
.... x = adict.get(num)
.... x = x + 1
# dont know how to update a value here
else:
// add key with value one here somehow.
Thats where I am at now.
For the record, I am not taking a class on python and using this forum
to do my homework. I graduated in 1999 and am trying to get familiar
with python and refresh my statistics skills. Python seems like it
would be a good language to do math/stat work in, in a lightweight way.

Thank for all the help and have a great day!
Ed
 
H

hawkesed

Actually,
I think I got it now. Here is what I did:.... if adict.has_key(num):
.... x = adict.get(num)
.... x = x + 1
.... adict.update({num:x})
.... else:
.... adict.update({num:1})
....{128: 2, 129: 2, 132: 1, 153: 1, 30: 1, 53: 1, 57: 1, 61: 1, 66: 2, 67:
1, 69: 2, 71: 1, 72: 1, 73: 2, 74: 1, 75: 2, 76: 2, 77: 2, 78: 2, 82:
1, 84: 1, 85: 4, 87: 2, 88: 3, 89: 1, 91: 1, 92: 1, 93: 2, 94: 2, 95:
3, 96: 1, 97: 3, 98: 2, 99: 1, 100: 6, 101: 4, 102: 2, 103: 1, 104: 1,
105: 1, 106: 2, 107: 2, 108: 2, 109: 2, 111: 3, 112: 1, 114: 3, 115: 3,
116: 3, 118: 1, 119: 2, 122: 2, 123: 1, 125: 1, 126: 1}
Thanks all for the help and best regards!
Ed
 
B

Brian van den Broek

hawkesed said unto the world upon 2005-04-21 20:28:
Actually,
I think I got it now. Here is what I did:


... if adict.has_key(num):
... x = adict.get(num)
... x = x + 1
... adict.update({num:x})
... else:
... adict.update({num:1})
...


{128: 2, 129: 2, 132: 1, 153: 1, 30: 1, 53: 1, 57: 1, 61: 1, 66: 2, 67:
1, 69: 2, 71: 1, 72: 1, 73: 2, 74: 1, 75: 2, 76: 2, 77: 2, 78: 2, 82:
1, 84: 1, 85: 4, 87: 2, 88: 3, 89: 1, 91: 1, 92: 1, 93: 2, 94: 2, 95:
3, 96: 1, 97: 3, 98: 2, 99: 1, 100: 6, 101: 4, 102: 2, 103: 1, 104: 1,
105: 1, 106: 2, 107: 2, 108: 2, 109: 2, 111: 3, 112: 1, 114: 3, 115: 3,
116: 3, 118: 1, 119: 2, 122: 2, 123: 1, 125: 1, 126: 1}
Thanks all for the help and best regards!
Ed


Hi Ed,

I think there is a more Pythonic way. Here's something I used in a
similar context:

def get_frequency_dict(sequence):
'''sequence of hashables -> dictionary of frequency of members

Given a sequence of items, each of which is hashable (i.e. can
serve as a key in a dictionary), the function returns a
dictionary, using the items as keys, and the number of their
occurrences as values.

Usage:
>>> get_frequency_dict([1,2,2,3,3,3,4,4,4,4]) {1: 1, 2: 2, 3: 3, 4: 4}
>>> get_frequency_dict([[], "will fail as list aren't hashable"])
...
TypeError: list objects are unhashable
'''
frequency_dict = {}

for element in sequence:
frequency_dict[element] = frequency_dict.setdefault(
element, 0) + 1

return frequency_dict

HTH,

Brian vdB
 
K

Kent Johnson

hawkesed said:
Actually,
I think I got it now. Here is what I did:


... if adict.has_key(num):
... x = adict.get(num)
... x = x + 1
... adict.update({num:x})

A simpler way to do this last line is
adict[num] = x
... else:
... adict.update({num:1})
and
adict[num] = 1

Kent
 
D

Dennis Lee Bieber

hawkesed said:
Actually,
I think I got it now. Here is what I did:


... if adict.has_key(num):
... x = adict.get(num)
... x = x + 1
... adict.update({num:x})

A simpler way to do this last line is
adict[num] = x
... else:
... adict.update({num:1})
and
adict[num] = 1

Kent

How about the very short:

adict[num] = adict.get(num, 0) + 1

If adict has a key of num, it retrieves the value, adds 1 to it,
and saves it back; otherwise, it returns a 0, adds 1, and saves as a new
key...

--
 
B

bserviss

A simple way to get individual values for the distribution is:

d = {}
for i in range( 0, 1000):
j = random.randrange( 0, 100)
if d.has_key(j):
d[j] += 1
else:
d[j] = 1

keys = d.keys()
keys.sort()
for key in keys:
print key, ":", "*" * d[key]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,235
Messages
2,571,181
Members
47,817
Latest member
BartHeckma

Latest Threads

Top