Typeerror: 'float' Object is not iterable in 2D CHOICE function

Asked

Viewed 750 times

0

I have the code below and I can’t make it work. For, I need an array from my 2D list to be chosen randomly with the CHOICE function and perform the sum of values that are less than 85.0 and subtract the values when greater. However, the following error occurs:

"Typeerror: 'float' Object is not iterable".

def escolher():
    from random import choice
    a = choice([[10.00,90.00,30.00,40.00],[50.00,60.00,90.00,80.00]])
    fitness_1_temp = 0
    for i in a:
        for j in i:
            if j <= 85.0:
                fitness_1_temp += j
            else:
                fitness_1_temp -= j
    return fitness_1_temp

print (escolher())

Where am I going wrong?

Thanks in advance for your help.

1 answer

1


from random import choice

def escolher():
    a = choice([[10.00,90.00,30.00,40.00],[50.00,60.00,90.00,80.00]])
    fitness_1_temp = 0
    for i in a:
        for j in i:
#---------------^^^
            if j <= 85.0:
                fitness_1_temp += j
            else:
                fitness_1_temp -= j
    return fitness_1_temp

print (escolher())

See in the code above, the part highlighted by the comment. You’re trying to iterate on a value of type float. Besides, it doesn’t make much sense to do both loop, only one is able to solve the sum.

Let’s say choice returned the first list:

a = [10.00,90.00,30.00,40.00]

Iterating for i in a, i will have the values 10.0, 90.0, 30.0 and 40.0, depending on the current iteration. From this value, you can already do the operation:

fitness_1_temp = (fitness_1_temp+i) if i <= 85.0 else (fitness_1_temp-i)

For the given examples, it will be returned -10.0 when the first list is considered or 100.0 when the second.

See working on Repl.it, in the Ideone, and in the Github Gist.

Workaround

When we want to reduce a list to a single value, we can use the operation reduce. In Python 2, reduce is a built-in Function, while in Python 3 it was incorporated into the module functools. Both have the same format:

reduce(function, iterable[, initializer])

In Python 2:

from random import choice

def escolher():
    a = choice([[10.00,90.00,30.00,40.00],[50.00,60.00,90.00,80.00]])
    fitness_1_temp = reduce(lambda x, y: (x+y) if y <= 85.0 else (x-y), a, 0)
    return fitness_1_temp

print (escolher())

In Python 3:

from random import choice
from functools import reduce

def escolher():
    a = choice([[10.00,90.00,30.00,40.00],[50.00,60.00,90.00,80.00]])
    fitness_1_temp = reduce(lambda x, y: (x+y) if y <= 85.0 else (x-y), a, 0)
    return fitness_1_temp

print (escolher())

Same solution, only need to import module function functools.

  • It worked perfectly buddy. Thank you very much!

  • @Danilo, I added an alternative solution using the function reduce. Take a look, it’s worth it.

  • I saw your solution with reduce and it is magnificent. You helped me immensely. You have no idea! A strong hug friend.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.