我正在尝试在字符串列表中搜索整数,并返回所搜索整数的元组列表。
text = ['10 KM, REDUCING TO 6KM, IN PASSING SHOWERS', '10 KM, REDUCING TO 4000 M, IN PASSING SHOWERS', '2500M IN RAIN']
我尝试了以下操作:
text_num = []
for i in text:
for x in i:
if x.isdigit() == True:
text_num.append(x)
返回:
text_num = ['1', '0', '6', '1', '0', '4', '0', '0', '0', '2', '5', '0', '0']
理想情况下,我希望输出是类似的东西。
text_num = [(10, 6), (10, 4000), (2500)]
答案 0 :(得分:0)
您可以使用正则表达式
import re
[[int(num) for num in re.findall(r'[0-9]+', string)]
for string in text]
输出
[[10, 6], [10, 4000], [2500]]