TransWikia.com

How to iterate over a list of floats in python

Stack Overflow Asked by JoeyHoward988 on January 27, 2021

I am trying to iterate over a list of floats in python which look like this

[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. I get the error ‘float’ 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.

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)

4 Answers

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. You then try to call sum on this singular float.

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.

Correct answer by orlp on January 27, 2021

You need to return

x**2

and

y**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

Answered by Aaj Kaal on January 27, 2021

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)

Answered by RufusVS on January 27, 2021

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. Remember that ight = sum(func(xpoint[i],ypoint[i])) returns the sum of the squares the two coordinates.

Answered by Gealber on January 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP