我有带分隔符的字符串列表A
and B
:
L = ['sgfgfqds A aaa','sderas B ffff','eeee','sdsdfd A rrr']
并需要:
L1 = [['aaa'], ['ffff'], ['eeee'], ['rrr']]
我尝试使用:
L1 = [re.findall(r'(?<=A)(.*)$', inputtext) for inputtext in L]
print (L1)
但是,它返回以下内容:
[[' aaa'], [], [], [' rrr']]
我怎样才能得到想要的输出?
您可以使用re.split https://docs.python.org/2/library/re.html#re.split打破你的琴弦'A'
or 'B'
:
>>> L1 = [re.split(r'[AB] *', inputtext)[-1] for inputtext in L]
>>> L1
['aaa', 'ffff', 'eeee', 'rrr']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)