[Tutor] if string contains dictionary key:Part Deux

Steve stevel@softhome.net
Thu, 10 Aug 2000 19:04:32 -0400


Thanks Wesley & Daniel! 
 I was going down that path but I wanted to get creative and use=
 the 
map or filter command.  I couldn't figure out how to pass it both=
 
string.split(s) and d.keys().

I also mis-represented the restraints of my program.  The string=
 
should have read
s=3D'This string has a CLR in it'
because I have another key name 'CLRc', 'CLR ' will always have a=
 
space following it.  The dictionary is defined by my program so I=
 can 
make it anything I want, but the string is from an input file. =
 So I 
can change my key from 'CLR ' to CLR'  and, with your help, I=
 came up 
with the following.

d =3D {'CLR': 'CLEAR', 'CA': 'ACCEPT', 'CR': 'CALL REQUEST'}
s=3D'This string has a CLR in it'

for x in string.split(s):
  if d.has_key(x): print d[x]

### or using map ###

def f(x):
  if d.has_key(x): print d[x]
map(f,string.split(s))