[Tutor] dict['_find']
Alan Gauld
alan.gauld at btinternet.com
Sun Feb 20 10:32:56 CET 2011
"Max Niederhofer" <max at niecap.com> wrote
> first post, please be gentle. I'm having serious trouble finding an
> alternative for the deprecated find module for dictionaries.
I think you are misunderstanding some of the terminology.
There is no deprecated find module. You are not using
any modules in your code. And you are not doing anything
special with "_fnd", it is just a string which you use as a
key in your dictionary.
> cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL':
> 'Jacksonville'}
>
> def find_city(themap, state):
> if state in themap:
> return themap[state]
> else:
> return "Not found."
>
> cities['_find'] = find_city
What this does is assign the function find_city as the value
corresponding to the key "_find" in your dictionary.
You could use any name you like:
'check' would be one meaningful example...
However, there is no obvious reason to put the
function in the dictionary at all...?
> while True:
> print "State? (ENTER to quit)",
> state = raw_input("> ")
>
> if not state: break
This loops round collecting state names from the user
and throwing them away until the user eventually gets
fed up and hits enter. At that point state holds an
empty string. I don't think you want that.
> city_found = cities['_find'](cities, state)
> print city_found
I think you want this inside the loop above...
> My question is - how do I rewrite this using an alternate module
> given
> find is deprecated?
'_find' is just a key, as such it is not deprecated.
What made you think it was? Did you get an error message?
If so always post the entire error text, it helps us enormously.
> reference, I'm using 2.6.1 on darwin.
With the slight change to the indentyation of the last two lines
your code should work.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list