问题描述
我有一些文本摘录,需要创建一个函数来查找文本中的所有数字并将其返回到浮点数列表中。
如果可以使用逗号分隔成千上万个并且可以用逗号和空格分隔几个连续的数字,那将是很好的选择
E.g.,extract_numbers("12 days of XMas") is [12.0]
E.g.,extract_numbers("1,2,3,un pasito pa'lante Maria")
is [1.0,2.0,3.0]
:param text: string that forms English text
:return: list of numbers (as floats) that are present in the text
:rtype: list
#Variable for storing the sum
a = 0
#Iterating through the content
#Of the file
for line in content:
for i in line:
# Checking for the digit in
# the string
if i.isdigit() == True:
a += int(i)
[float(i) for i in a]
print("The sum is:",a)
很不幸,我收到TypeError:
'int' object is not iterable' at '[float(i) for i in a]'
解决方法
a
是int且不可迭代。您可以使用:
a = 0
floats = []
for line in content:
for i in line:
if i.isdigit() == True:
a += int(i)
floats.append(float(i))
现在floats
是一个包含所有数字的列表。
使用regular expression,您的extract_numbers
函数可能看起来像这样:
import re
def extract_numbers(line):
return [float(num) for num in re.findall(r'\d+',line)]
然后您可以执行以下操作:
# Variable for storing the sum
a = 0
# Iterating through the content of the file
for line in content:
a += extract_numbers(line)
print("The sum is:",a)