# Python3 code to demonstrate
# getting numbers from string
# using List comprehension + isdigit() +split()
# initializing string
test_string = "There are 2 Cars for 8 persons"
# printing original string
print("The original string : " + test_string)
# using List comprehension + isdigit() +split()
# getting numbers from string
res = [int(i) for i in test_string.split() if i.isdigit()]
# print result
print("The numbers list is : " + str(res))
这段代码以任何方式从字符串中提取数字
希望这能帮助你解决你的问题
使用“”作为拆分位置调用拆分函数,然后转换为int:
相关问题 更多 >
编程相关推荐