[Tutor] dictionary values

luke p rabidpoobear at gmail.com
Sat Jul 9 01:40:18 CEST 2005


just assume all the below code is correct.
I am not having a problem with it, it is all for example only.

I have a dictionary like this:
alpha = {'a':0,'b':0, ... 'z':0}
and the following code
f = file("hamlet.txt","r")
text = f.readlines()
f.close()
for line in text:
  for char in line:
    try:
      alpha[char] += 1

so at the end of the loop I have a dictionary eg.
{'a':14000,'b':12000 ... 'z':100}

what I want to do is find out which value in my dictionary is lowest.
is there a dictionary function for this, like alpha.min() that will
return a key:value pair of the lowest? I cannot find one and I
wondered if there was a quick fix to this.

what I will do instead to find the lowest is just use a list instead...

alphalist = ['a','b' ... 'z']
alphavalues = [0,0 ... 0]
lowest = alphavalues[0]
lowestlocation = 0
and just do
for x in range(26):#or is it 25? can't remember if value is included
  if alphavalues[x] < lowest:
    lowest = alphavalues[x]
    lowestlocation = x

but for future reference I just wondered about the dictionary thing.
thanks in advance.
-Luke


More information about the Tutor mailing list