[Tutor] Using re/search with dictionaries

Jackson david.jay.jackson@wcox.com
Wed, 31 Oct 2001 10:04:34 -0700


Greetings --
How do I search dict keys? and return the records that go along with them? It seems as if it should be something along this line:

# we're looking for keys/records with blue* in them.
m=rd.search("\w[blue]+",d.keys())  
if m: print repr(m.group(0))
   and the output should be:

[blue-001"]=["blueberry pie"]
["blue-002"]=["blueberry cobbler"]
["blue-003"]=["blueberry muffins"]
...
....


Thanks for you time
David Jackson

--------------- create dict script ----------------------
#!/usr/bin/python
import re 
d = {}
d["blue-001"]=["blueberry pie"]
d["blue-002"]=["blueberry cobbler"]
d["blue-003"]=["blueberry muffins"]
d["cherry-001"]=["cherry pie"]
d["cherry-002"]=["wild cherry icecream"]
d["blueberry"]=["muffins"]
d["bluebird"]=["feathered friend"]
d["red red robin"]=["goes bob bob bobing along"]