Python 提取数字并从列表中添加它们
Python Extracting numbers and adding them from a list
我有一个字符串列表:
[' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n', ' 43 miles\n', ' 15.0 miles\n', ' 0.0 miles\n', ' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n', '.7 miles', '5. miles']`
所以有不同格式的数字:
86
.7
5.
0.0
后跟文本字符串。
我需要将此列表中的所有数字相加。
添加部分显然很容易,但我如何只将数字放入自己的列表中?
您可以使用正则表达式将您的列表转换为包含数字的列表:
import re
regex = re.compile("[-+]?[0-9]*\.?[0-9]+")
l = [' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n', ' 43 miles\n', ' 15.0 m iles\n', ' 0.0 miles\n', ' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n']
numbers = [float(regex.findall(x)[0]) for x in l if regex.findall(x) != [] ]
>>> [86.0, 43.0, 0.0, 43.0, 15.0, 0.0, 0.0, 0.0, 86.0]
我们可以将所有字符串合并为一个,使用正则表达式提取所有数字,然后将它们相加:
>>> import re
>>> l = [
' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n',
' 43 miles\n', ' 15.0 miles\n', ' 0.0 miles\n',
' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n'
]
>>> sum(map(float, re.findall(r"\d+(?:\.\d+)?", "".join(l))))
273.0
假设您希望数字为浮点数:
miles = [' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n', ' 43 miles\n', ' 15.0 miles\n', ' 0.0 miles\n', ' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n']
total = 0.0
for s in miles:
for t in s.split():
try:
total += float(t)
except ValueError:
pass
>>> import re
>>> elements = [' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n', ' 43 miles\n', ' 15.0 m iles\n', ' 0.0 miles\n', ' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n', ' .7 miles', ' 5. miles']
>>> _re_digits = re.compile(r"(-?(?:(?:\d+(?:\.\d*)?)|(?:\.\d+)))")
>>> digits = []
>>> for element in elements:
digits += [ float(n) for n in _re_digits.findall(element)]
>>> digits
[86.0, 43.0, 0.0, 43.0, 15.0, 0.0, 0.0, 0.0, 86.0, 0.7, 5.0]
>>> sum(digits)
278.7
我有一个字符串列表:
[' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n', ' 43 miles\n', ' 15.0 miles\n', ' 0.0 miles\n', ' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n', '.7 miles', '5. miles']`
所以有不同格式的数字:
86
.7
5.
0.0
后跟文本字符串。
我需要将此列表中的所有数字相加。
添加部分显然很容易,但我如何只将数字放入自己的列表中?
您可以使用正则表达式将您的列表转换为包含数字的列表:
import re
regex = re.compile("[-+]?[0-9]*\.?[0-9]+")
l = [' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n', ' 43 miles\n', ' 15.0 m iles\n', ' 0.0 miles\n', ' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n']
numbers = [float(regex.findall(x)[0]) for x in l if regex.findall(x) != [] ]
>>> [86.0, 43.0, 0.0, 43.0, 15.0, 0.0, 0.0, 0.0, 86.0]
我们可以将所有字符串合并为一个,使用正则表达式提取所有数字,然后将它们相加:
>>> import re
>>> l = [
' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n',
' 43 miles\n', ' 15.0 miles\n', ' 0.0 miles\n',
' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n'
]
>>> sum(map(float, re.findall(r"\d+(?:\.\d+)?", "".join(l))))
273.0
假设您希望数字为浮点数:
miles = [' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n', ' 43 miles\n', ' 15.0 miles\n', ' 0.0 miles\n', ' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n']
total = 0.0
for s in miles:
for t in s.split():
try:
total += float(t)
except ValueError:
pass
>>> import re
>>> elements = [' 86 miles\n', ' 43 miles\n', ' MV\n', ' 0.0 miles\n', ' 43 miles\n', ' 15.0 m iles\n', ' 0.0 miles\n', ' 0.0 miles\n', ' 0.0 miles \n', ' 86 miles\n', ' .7 miles', ' 5. miles']
>>> _re_digits = re.compile(r"(-?(?:(?:\d+(?:\.\d*)?)|(?:\.\d+)))")
>>> digits = []
>>> for element in elements:
digits += [ float(n) for n in _re_digits.findall(element)]
>>> digits
[86.0, 43.0, 0.0, 43.0, 15.0, 0.0, 0.0, 0.0, 86.0, 0.7, 5.0]
>>> sum(digits)
278.7