[Tutor] dict['_find']
Knacktus
knacktus at googlemail.com
Sun Feb 20 10:40:18 CET 2011
Am 20.02.2011 05:14, schrieb Max Niederhofer:
> Hello all,
Hello Max,
>
> first post, please be gentle. I'm having serious trouble finding an
> alternative for the deprecated find module for dictionaries.
>
> The code (from Zed Shaw's Hard Way, exercise 40) goes something like
> this. Hope indentation survives.
>
> cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'}
I use a naming convention for dicts that has made me very happy on
several occasion ;-):
key_to_value, in your case
state_to_city = {...}
>
> def find_city(themap, state):
> if state in themap:
> return themap[state]
> else:
> return "Not found."
>
> cities['_find'] = find_city
Did you put this entry into the same dictionary as the data on purpose?
Or is the purpose a kind of dispatch? Something that could be a dict on
its own, like
private_function_name_to_function = {'_find': find_city}
You should try to keep things seperate and explicit.
>
> while True:
> print "State? (ENTER to quit)",
> state = raw_input("> ")
>
> if not state: break
>
> city_found = cities['_find'](cities, state)
> print city_found
>
> My question is - how do I rewrite this using an alternate module given
> find is deprecated? Grateful for all suggestions or pointers. For
> reference, I'm using 2.6.1 on darwin.
>
> Thanks so much for your help.
>
> Best,
> Max
>
> --
> Dr. Maximilian Niederhofer
> Founder, Qwerly
> http://qwerly.com/ | http://qwerly.com/max
> +44 78 3783 8227
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list