如何在 python 列表中添加浮点数?
How to add the floating numbers in a list in python?
如何在输入文件中添加平方和?
输入文件是一个txt文件,如下所示:
10 9 8 7 1 2 3 4
3 -4 1 -2
0.743 -12.3 5.3333 3
-3.3
因此输出可能如下所示;
324.0
30.0
189.28613789000002
10.889999999999999
我无法使用 sum 添加浮点数,因为它显示错误,因此非常感谢您的帮助。
这是我的代码:
#Ask the user to input a file name
file_name=input("Enter the Filename: ")
#Opening the desired file to read the content
infile=open(file_name,'r')
#Importing the math library
import math
#Iterating for the number of lines in the file
for line in infile:
#Converting the file to a list row by row
line_str=line.split()
for element in range(len(line_str)):
line_str[element]=float(line_str[element])
line_str[element]=math.pow(line_str[element],2)
total=sum(line_str[0:len(element)])
print(total)
for line in infile:
numbers = map(float, line.split())
squares = (i ** 2 for i in numbers)
print(sum(squares))
您的代码存在多个问题。
你得到的错误是因为你做了:
total=sum(line_str[0:len(element)])
元素是一个整数,因为它循环遍历 range()
生成的值。
您有 total
化并在 for 循环中打印。您为每个元素而不是每一行调用 print
,因此您无法获得所需的输出。
这:line_str[0:0]
会给你一个空列表,你使用这个:line_str[0:element]
永远不会包括最后一个元素
这是一个仅需少量改动的版本:
#Ask the user to input a file name
# file_name=input("Enter the Filename: ") # temporary commented out for easier testing.
file_name = "input.txt"
#Opening the desired file to read the content
infile=open(file_name,'r')
#Importing the math library
import math
#Iterating for the number of lines in the file
for line in infile:
#Converting the file to a list row by row
line_str=line.split()
for element in range(len(line_str)):
line_str[element]=float(line_str[element])
line_str[element]=math.pow(line_str[element],2)
total=sum(line_str[0:element+1])
print(total)
这给出:
324.0
30.0
189.28613789000002
10.889999999999999
但可以大大改进:
- 在作业中
=
使用 space 秒
- 不要使用
for .... range(somelist):
然后索引 somelist
。而是使用枚举。
- 将转换为 float 与拆分行相结合
- 在评论中的“#”后使用 space
大致如下:
# Ask the user to input a file name
# file_name=input("Enter the Filename: ")
file_name = "input.txt"
# Opening the desired file to read the content
infile=open(file_name,'r')
# Importing the math library
import math
# Iterating for the number of lines in the file
for line in infile:
# Converting the file to a list row by row and convert to float
line_str = [float(x) for x in line.split()]
for idx, element in enumerate(line_str):
line_str[idx] = math.pow(element,2)
total = sum(line_str)
print(total)
我认为您误解了 split 函数及其作用。
$ python
Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> line = "10 9 8 7 1 2 3 4"
>>> line_str = line.split()
>>> line_str
['10', '9', '8', '7', '1', '2', '3', '4']
split 获取字符串并在每个 space 将字符串拆分为子字符串,每个子字符串都是列表中的一个元素
您的循环代码表现得好像您需要在字符串中查找各个数字。
>>> for element in range(len(line_str)):
... line_str[element]=float(line_str[element])
... line_str[element]=math.pow(line_str[element],2)
... total=sum(line_str[0:len(element)])
... print(total)
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
TypeError: object of type 'int' has no len()
现在让我们遍历列表(使用上面建议的枚举)并将每个元素转换为浮点数,使用 math.pow 将其平方并将其存储回同一个列表中。您可以将列表传递给 sum() 以获得总数。 Python 的美妙之处在于您可以随时进入解释器并 运行 您的命令一条一条地调查结果。
>>> import math
>>> line = "10 9 8 7 1 2 3 4"
>>> line_str = line.split()
>>> line_str
['10', '9', '8', '7', '1', '2', '3', '4']
>>> for i,value in enumerate(line_str):
... fvalue = float(value)
... line_str[i] = math.pow(fvalue,2)
...
>>> line_str
[100.0, 81.0, 64.0, 49.0, 1.0, 4.0, 9.0, 16.0]
>>> sum(line_str)
324.0
>>>
现在您有一天可能会问自己的问题是,为什么我要使用 math.pow(fvalue,2)
,而我本可以使用 fvalue**2
甚至 fvalue*fvalue
准备好后,您可以在此处阅读相关内容 Exponentials in python x.**y vs math.pow(x, y)
如何在输入文件中添加平方和?
输入文件是一个txt文件,如下所示:
10 9 8 7 1 2 3 4
3 -4 1 -2
0.743 -12.3 5.3333 3
-3.3
因此输出可能如下所示;
324.0
30.0
189.28613789000002
10.889999999999999
我无法使用 sum 添加浮点数,因为它显示错误,因此非常感谢您的帮助。
这是我的代码:
#Ask the user to input a file name
file_name=input("Enter the Filename: ")
#Opening the desired file to read the content
infile=open(file_name,'r')
#Importing the math library
import math
#Iterating for the number of lines in the file
for line in infile:
#Converting the file to a list row by row
line_str=line.split()
for element in range(len(line_str)):
line_str[element]=float(line_str[element])
line_str[element]=math.pow(line_str[element],2)
total=sum(line_str[0:len(element)])
print(total)
for line in infile:
numbers = map(float, line.split())
squares = (i ** 2 for i in numbers)
print(sum(squares))
您的代码存在多个问题。
你得到的错误是因为你做了:
total=sum(line_str[0:len(element)])
元素是一个整数,因为它循环遍历
range()
生成的值。您有
total
化并在 for 循环中打印。您为每个元素而不是每一行调用print
,因此您无法获得所需的输出。这:
line_str[0:0]
会给你一个空列表,你使用这个:line_str[0:element]
永远不会包括最后一个元素
这是一个仅需少量改动的版本:
#Ask the user to input a file name
# file_name=input("Enter the Filename: ") # temporary commented out for easier testing.
file_name = "input.txt"
#Opening the desired file to read the content
infile=open(file_name,'r')
#Importing the math library
import math
#Iterating for the number of lines in the file
for line in infile:
#Converting the file to a list row by row
line_str=line.split()
for element in range(len(line_str)):
line_str[element]=float(line_str[element])
line_str[element]=math.pow(line_str[element],2)
total=sum(line_str[0:element+1])
print(total)
这给出:
324.0
30.0
189.28613789000002
10.889999999999999
但可以大大改进:
- 在作业中
=
使用 space 秒 - 不要使用
for .... range(somelist):
然后索引somelist
。而是使用枚举。 - 将转换为 float 与拆分行相结合
- 在评论中的“#”后使用 space
大致如下:
# Ask the user to input a file name
# file_name=input("Enter the Filename: ")
file_name = "input.txt"
# Opening the desired file to read the content
infile=open(file_name,'r')
# Importing the math library
import math
# Iterating for the number of lines in the file
for line in infile:
# Converting the file to a list row by row and convert to float
line_str = [float(x) for x in line.split()]
for idx, element in enumerate(line_str):
line_str[idx] = math.pow(element,2)
total = sum(line_str)
print(total)
我认为您误解了 split 函数及其作用。
$ python
Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> line = "10 9 8 7 1 2 3 4"
>>> line_str = line.split()
>>> line_str
['10', '9', '8', '7', '1', '2', '3', '4']
split 获取字符串并在每个 space 将字符串拆分为子字符串,每个子字符串都是列表中的一个元素
您的循环代码表现得好像您需要在字符串中查找各个数字。
>>> for element in range(len(line_str)):
... line_str[element]=float(line_str[element])
... line_str[element]=math.pow(line_str[element],2)
... total=sum(line_str[0:len(element)])
... print(total)
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
TypeError: object of type 'int' has no len()
现在让我们遍历列表(使用上面建议的枚举)并将每个元素转换为浮点数,使用 math.pow 将其平方并将其存储回同一个列表中。您可以将列表传递给 sum() 以获得总数。 Python 的美妙之处在于您可以随时进入解释器并 运行 您的命令一条一条地调查结果。
>>> import math
>>> line = "10 9 8 7 1 2 3 4"
>>> line_str = line.split()
>>> line_str
['10', '9', '8', '7', '1', '2', '3', '4']
>>> for i,value in enumerate(line_str):
... fvalue = float(value)
... line_str[i] = math.pow(fvalue,2)
...
>>> line_str
[100.0, 81.0, 64.0, 49.0, 1.0, 4.0, 9.0, 16.0]
>>> sum(line_str)
324.0
>>>
现在您有一天可能会问自己的问题是,为什么我要使用 math.pow(fvalue,2)
,而我本可以使用 fvalue**2
甚至 fvalue*fvalue
准备好后,您可以在此处阅读相关内容 Exponentials in python x.**y vs math.pow(x, y)