Cannot multiply the sequence by a non-int of type 'float', even when transformed into 'map'

Asked

Viewed 71 times

-1

level: beginner

I need to multiply that [list] * [float]:

eta = [random.random () - 0,5] * eps

but when I try to make a float or a map, it still doesn’t work (because of unsupported operand types). is there another way to do this? thank you.

  • Welcome to Stackoverflow in English. As the name suggests, the official language used here is Portuguese. So, could you please translate your question? If you prefer, you can also ask the same question on the Stackoverflow website.

1 answer

-1

Python’s default multiplication of a Sequence is "concatenate the Sequence with itself N times" - and is therefore defined only for sequences X integers.

Apparantly you intend to have each Member of the Sequence multiplied in place, and may have overheard someone telling you to use map.

map is a built-in to perform one Operation on each Member of a Sequence, and it would work like this:

new_list = list(map(lambda element: element * float_value, original_list))

Python has a short syntax for that in the form of list comprehensions, Where one can put an Expression directly, Instead of Passing a Function as a Parameter to map:

new_list = [element * float_value for element in original_list]
  • 1

    Welcome to Stackoverflow in English. As the name suggests, the official language used here is Portuguese. So, could you please translate your question? If you prefer, you can also ask the same question on the Stackoverflow website

  • it Happens: Unsupported operand type(s) for +: 'float' and 'list'

  • and what went wrong when you use as I showed above instead of new_list = orignal_list * value?

Browser other questions tagged

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