我有一个巨大的列表,其中包含多个元素,如下所示:
'A.21,48;B.64,91;C.95,125;D.139,166;E.175,200'
我想删除所有字母和; .
个字符并附加到列表中,使列表看起来像['21','48','64','91','95','125','139','166','175','200']
我尝试使用:
import re
list.append(re.sub("\D","", <the string i am working on>))
但会产生如下列表 - [' 21 48 64 91 95 125 139 166 175 200']
由于我在一个非常大的文件循环中工作,因此单行代码将非常有用。
谢谢
答案 0 :(得分:3)
您可以使用re.findall()
:
>>> import re
>>> strs='A.21,48;B.64,91;C.95,125;D.139,166;E.175,200'
>>> re.findall(r"\d+",strs)
['21', '48', '64', '91', '95', '125', '139', '166', '175', '200']
使用re.sub()
:
>>> re.sub(r"[A-Z;,.]"," ",strs).split()
['21', '48', '64', '91', '95', '125', '139', '166', '175', '200']
帮助(re.findall):强>
Help on function findall in module re:
findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result.