Alex Martelli <aleax at aleax.it> -> search re and put in a list??

jubafre at brturbo.com jubafre at brturbo.com
Mon Sep 16 21:45:11 CEST 2002


mem=['7','8', '9']
labels=['D1','D2','D3'] 
S=['20 D1', '30 D2', '10 D3', 'F0', ' 3', ' 2', ' 0']

i want final=['20 7', '30 8', '10 9', 'F0', ' 3', ' 2', ' 0']

#++++++++++++++++++++++++++++++++++++++++++++++
repdict = dict(zip(labels, mem))
big_re = re.compile('|'.join(labels))
def replacer(mo, repdict=repdict):
    return repdict[mo.group()]
final = [ big_re.sub(x, replacer)for x in S ]
print final 
#++++++++++++++++++++++++++++++++++++++++++++++

 Traceback (most recent call last):
  File "<string>", line 1, in ?
  File "C:\Documents and Settings\jubafre\Desktop\teste.py", line 27, in ?
    final = [ big_re.sub(x, replacer)for x in S ]
TypeError: expected string or buffer
	

what happened????



Juliano Freitas
www.gebrasil.hpg.com.br 


More information about the Python-list mailing list