Function to prune dictionary keys not working

Terry Reedy tjreedy at udel.edu
Tue Jun 27 22:22:03 EDT 2006


"Girish Sahani" <girish at cse.iitb.ac.in> wrote in message 
news:52244.10.209.4.2.1151460911.squirrel at 10.105.1.3...
> hi ppl,
> Here is a simple function to remove those keys of a dictionary whose
> values are less than some specified value. But it isnt working. Please
> help.
>
> def prune(d,cp):
>    l = []
>    for rule,value in d.iteritems():
>        #print value
>        if value >= cp:
>            l.append(rule)
>    return l
>
>
>>>> d = {'be=>c': '1.00', 'c=>da': '0.50', 'ea=>b': '0.33', 'be=>d':
> '0.50', 'c=>ba': '0.33', 'bd=>a': '1.00', 'a=>cb': '0.33', 'ea=>c':
> '0.67', 'a=>cd': '0.50', 'e=>ac': '0.40', 'e=>ab': '0.20', 'c=>bd':
> '0.33', 'e=>cb': '0.40', 'ed=>b': '0.25', 'ed=>c': '0.50'}
>>>> cp = 0.5
>>>> if prune(d,cp) == d.keys():
> print "code not working :(("
>
> code not working :((

You are comparing a float (0.5) to strings ('1.00', etc).  The result of 
that comparison is undefined.  Unquote the values and try again.

tjr






More information about the Python-list mailing list