1

我有一个巨大的列表,其中包含多个元素,如下所示:

'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']

由于我在一个非常大的文件的循环中工作,因此一行代码将非常有帮助。

谢谢

4

1 回答 1

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']

帮助(重新.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.
于 2013-03-20T21:12:10.990 回答