【发布时间】:2018-03-23 15:10:49
【问题描述】:
我有一个带有文本和数字的字符串,例如:
string = "Hello this is 123 456 a string for test 12 345 678. I want to merge 12 34 56"
我只想将连续的数字放在一起,如下所示:
newString = "Hello this is 123456 a string for test 12345678. I want to merge 123456"
如何检测数字,检查它们是否连续并将它们连接起来?
谢谢!
【问题讨论】:
-
我首先尝试获取如下数字: string = "Hello this is 123 456 a string for test 12 345 678. I want to merge 12 34 56" [int(s) for s in str.split() if s.isdigit()]
标签: python string numbers concatenation