[Tutor] dictionary values

Don Parris gnumathetes at gmail.com
Sat Jul 9 02:09:17 CEST 2005


On 7/8/05, luke p <rabidpoobear at gmail.com> wrote:
> 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
> _______________________________________________


I'm new at this, but thought I would throw my $0.02 in the ring for
the learning experience.  I know that sequence operations won't work
on dictionaries.  I wonder if you could get away with a lambda here? 
I'm probably in way over my head, but would something like this work:

min = (lambda x, y: x < y)
min(1, 9)

Could min() take the dictionary values, as in min(dict[0], dict[9])?

Again, I'm no expert, but having my input picked apart will be a good thing. ;)

Don
-- 
DC Parris GNU Evangelist
http://matheteuo.org/
gnumathetes at gmail.com
Free software is like God's love - 
you can share it with anyone anywhere anytime!


More information about the Tutor mailing list