help - iter & dict

aking at mappi.helsinki.fi aking at mappi.helsinki.fi
Thu Aug 3 15:10:43 EDT 2006



Dear Python people,

im a newbie to python and here...so hello!

Im trying to iterate through values in a dictionary so i can find the
closest value and then extract the key for that value....what ive done so far:

def pcloop(dictionary, exvalue):
        z = dictionary.itervalues()
        y = z - exvalue
        v = (y*y)**1/2
        if v < 0.001:
            u = dictionary.get[z]
        return u


ive been working off a couple of books and this is the best i can get it in
short time. I was trying to define a function (its my first!) so that i
could apply to several 'dictionary's and 'exvalue's. The best ive been able
to come up with is iterating over the dictionary values, subtracting the
exvalue, squaring then root squaring to render positive and applying an
error (0.001) which is not the ideal solution i want. Is there any easy way
to iterate through dictionary values and return the key for the minimum. Or
can someone tell me where im going wrong with this def & loop.

regards all

Ali



More information about the Python-list mailing list