[英]How to iterate over a list of floats in python
I am trying to iterate over a list of floats in python which look like this我正在尝试遍历 python 中的浮点列表,看起来像这样
[3.09312635991831, 2.685707263863166, 1.7249614343269735, 2.551923173039131, 2.3768648392514904]
and plug them into a function handle but I cant seem to iterate over the list of floats.并将它们插入 function 句柄,但我似乎无法遍历浮动列表。 I get the error 'float' object is not iterable.
我收到错误“浮动”object is not iterable。 Here is what I am currently working with.
这是我目前正在使用的。 Where
xpoint
and ypoint
are the lists of floats and func
is the expression func = lambda x,y: x**2 + y**2
.其中
xpoint
和ypoint
是浮点数列表, func
是表达式func = lambda x,y: x**2 + y**2
。
def monte_carlo_integral(a,b,c,n,func):
T = triangle_area(a, b, c)
left = T*(1/n)
points = random_numbers(a, b, c, n, 1)
xpoint = [item[0] for item in points]
ypoint = [item[1] for item in points]
for i in range(n):
right = sum(func(xpoint[i],ypoint[i]))
print(right)
I reproduced the problem here.我在这里重现了这个问题。
func = lambda x,y: x**2 + y**2
x = [2.9326170613860096, 1.8654993478646646, 2.878679040963291, 1.2534822780679544, 2.9724667148405075]
y = [3.7757464252380686, 1.1428831196926126, 2.107894507044161, 1.8353208404114492, 3.868876505529935]
for i in range(5):
summation = sum(func(x[i],y[i]))
print(summation)
func = lambda x,y: x**2 + y**2
for i in range(5):
summation = sum(func(x[i],y[i]))
Your problem is that func(x[i],y[i])
returns a single float.您的问题是
func(x[i],y[i])
返回一个浮点数。 You then try to call sum
on this singular float.然后,您尝试在这个单数浮点数上调用
sum
。
What you probably want instead is this:你可能想要的是这样的:
func = lambda x,y: x**2 + y**2
summation = 0
for i in range(5):
summation += func(x[i],y[i])
Or even better, this:或者更好的是,这个:
func = lambda x,y: x**2 + y**2
summation = sum(func(x[i],y[i]) for i in range(5))
Or even better, this:或者更好的是,这个:
summation = sum(xi**2 + yi**2 for xi, yi in zip(x, y))
Also note the math.hypot
function.还要注意
math.hypot
function。
The problem I think is in this line我认为问题出在这一行
right = sum(func(xpoint[i],ypoint[i]))
the argument of the function sum
must be an iterable object, but you're passing a float
. function
sum
的参数必须是可迭代的 object,但您传递的是float
。 Remember that ight = sum(func(xpoint[i],ypoint[i]))
returns the sum of the squares the two coordinates.请记住
ight = sum(func(xpoint[i],ypoint[i]))
返回两个坐标的平方和。
You probably want one of the two solutions Here您可能需要以下两种解决方案之一
Setup:设置:
func = lambda x,y: x**2 + y**2
x = [2.9326170613860096, 1.8654993478646646, 2.878679040963291, 1.2534822780679544,
2.9724667148405075]
y = [3.7757464252380686, 1.1428831196926126, 2.107894507044161, 1.8353208404114492,
3.868876505529935]
Solution One:解决方案一:
summation = 0
for i in range(5):
summation += func(x[i],y[i])
Solution Two:解决方案二:
summation = sum((func(x[i],y[i]) for i in range(5)))
Print result:打印结果:
print(summation)
You need to return你需要返回
x**2
x**2
and和
y**2
是**2
which you can them sum as follows:你可以总结如下:
func = lambda x,y: ((x**2),(y**2))
x = [2.9326170613860096, 1.8654993478646646, 2.878679040963291, 1.2534822780679544, 2.9724667148405075]
y = [3.7757464252380686, 1.1428831196926126, 2.107894507044161, 1.8353208404114492, 3.868876505529935]
for a,b in zip(x,y):
summation += sum(func(a,b))
print(summation) # 92.91993379273195
use the map function使用 map function
func = lambda x,y: x**2 + y**2
x = [2.9326170613860096, 1.8654993478646646, 2.878679040963291,
1.2534822780679544,
2.9724667148405075]
y = [3.7757464252380686, 1.1428831196926126, 2.107894507044161,
1.8353208404114492,
3.868876505529935]
print(list(map(func,x,y)))
output: output:
[22.856503896430368, 4.786269642161807, 12.73001227370828, 4.9396204086790165, 23.80376378587624]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.