问题:
一个字符串 ''1 adfa fa 2 fafsa 3 adfaf''
一个列表['one','two','three']
怎样将字符串的1,2,3用下面的列表中对应的元素替换
代码解决:
import re def replace(match): global i, list rep = list[i] i += 1 return rep str = '1 adfa fa 2 fafsa 3 adfaf' list = ['one', 'two', 'three'] pat = re.compile(r'\d+') i = 0 print pat.sub(replace, str)