AnswerBun.com

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

[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)

Stack Overflow Asked by JoeyHoward988 on January 27, 2021

4 Answers

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!

Related Questions

Flight destination check programm – C++

3  Asked on January 12, 2021 by mrfreeman16

   

How to execute (./myscript) inside awk or bash script?

3  Asked on January 12, 2021 by redouane-nouh

   

Disable nuxt link based on boolean

3  Asked on January 12, 2021 by samantha

     

Android Preventing Double Click On A Layout with handher

1  Asked on January 12, 2021 by user13380366

     

How to center align the label text inside the option tag?

1  Asked on January 11, 2021 by mr-spock

         

How can I read the CSS for mobile images

0  Asked on January 11, 2021 by power-rico

   

Deleting from array using unset creates values

2  Asked on January 11, 2021 by cameron

 

passport.socket.io cant fix no session found

2  Asked on January 11, 2021 by edi_hadzic

         

Output should look like a quotation

4  Asked on January 11, 2021 by maurya

   

Sorting DateTime column in ASP.NET MVC

2  Asked on January 11, 2021 by steven85791

     

Rounding does not match all expected results

1  Asked on January 11, 2021 by aa-44

   

Django tabular input form (user can key in data into table)

1  Asked on January 11, 2021 by eka-buyung-lienadi

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.