我是python正则表达式的初学者.我达到了我所需要的,但是由于缺少经验,这真的很丑.我的目标是转换以下形式的字符串数组:
notes = ["10.0% higher", "5.0% lower", "Same as", "21.2% lower"]
到一个float数组,这样上面的数组将产生:
changes = [10.0,-5.0,0,-21.2]
下面的代码实现了这一点,但确实是重复的和糟糕的样式.我该如何优化呢?
changes = []
for note in notes:
m = re.search(r"(?:(\d+\.\d+\%\shigher)|(\d+\.\d+\%\slower)|(Same\sas))", note)
if m:
if m.groups(0):
if m.groups(0)[0]:
changes += [float(re.match(r"(\d+\.\d+)", m.groups(0)[0]).groups(0)[0])]
elif m.groups(0)[1]:
changes += [-float(re.match(r"(\d+\.\d+)", m.groups(0)[1]).groups(0)[0])]
else:
changes += [0.0]
print changes
解决方法:
使用findall可以在单个正则表达式中执行此操作:
notes = ["10.0% higher", "5.0% lower", "Same as", "21.2% lower"]
changes = []
for note in notes:
m = re.findall("(?:(\d+\.\d+)% )?(higher|lower|Same as)", note)
if len(m):
if m[0][1] == 'higher':
changes += [float(m[0][0])]
elif m[0][1] == 'lower':
changes += [-float(m[0][0])]
else:
changes += [0.0]
print changes
标签:python,regex