我有一个字符串
Mystring = "123 456 789, 234, 999|567 888[222"
我想将此字符串拆分为" "
","
"|"
和"["
以获取列表中的所有数字
预期产出: -
List = ["123","456","789","234","999",567","888","222"]
我正在使用以下代码
Final_List = re.findall("(\d+?)[ ,|\]]",Mystring)
实际输出:
["123","456","789",234","999",567"]
我如何在这里获得所有数字?
答案 0 :(得分:5)
为什么不直接获取数字,而不是分割字符串:
>>> import re
>>> Mystring = "123 456 789, 234, 999|567 888[222"
>>> re.findall('\d+', Mystring)
['123', '456', '789', '234', '999', '567', '888', '222']
>>>
\d+
Python匹配一个或多个数字(数字)。
答案 1 :(得分:1)
import string
tbl = string.maketrans(',|['," ")
print(Mystring.translate(tbl)).split()
['123', '456', '789', '234', '999', '567', '888', '222']
In [29]: import string
In [30]: %%timeit
....: tbl = string.maketrans(',|['," ")
....: (Mystring.translate(tbl)).split()
....:
1000000 loops, best of 3: 762 ns per loop
In [31]: import re
In [32]: timeit re.findall('\d+', Mystring)
100000 loops, best of 3: 2.99 µs per loop