Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to solve TypeError: 'float' object is not iterable

How can I transfer

A = [0.12075357905088335, -0.192198145631724, 0.9455373400335009, -0.6811922263715244, 0.7683786941009969, 0.033112227984689206, -0.3812622359989405] 

to

A = [[0.12075357905088335], [-0.192198145631724], [0.9455373400335009], [-0.6811922263715244], [0.7683786941009969], [0.033112227984689206], [-0.3812622359989405]]

I tried to the code below but an error occurred:

new = []
for i in A:
    new.append.list(i)

TypeError: 'float' object is not iterable

Could anyone help me?

like image 494
FlyingBurger Avatar asked Apr 24 '18 09:04

FlyingBurger


People also ask

How do you fix an object not iterable?

How to Fix Int Object is Not Iterable. One way to fix it is to pass the variable into the range() function. In Python, the range function checks the variable passed into it and returns a series of numbers starting from 0 and stopping right before the specified number.

Why is a float not iterable?

Explanation: In the above example, we are trying to iterate through a 'for loop' using a float value. But the float is not iterable. Float objects are not iterable because of the absence of the __iter__ method.

How do I iterate a float object in Python?

Import numpy module using the import numpy as np statement. Pass float numbers to its start, stop, and step argument. For example, np. arange(0.5, 6.5, 1.5) will return the sequence of floating-point numbers starting from 0.5 up to 6.5.

What does object is not iterable mean?

They occur when you try to apply a function on a value of the wrong type. An “'int' object is not iterable” error is raised when you try to iterate over an integer value. To solve this error, make sure that you are iterating over an iterable rather than a number.


1 Answers

tl;dr

Try list comprehension, it is much more convenient:

new = [[i] for i in A]

Explanation

You are getting TypeError because you cannot apply list() function to value of type float. This function takes an iterable as a parameter and float is not an iterable.

Another mistake is that you are using new.append._something instead of new.append(_something): append is a method of a list object, so you should provide an item to add as a parameter.

like image 85
Ivan Vinogradov Avatar answered Oct 04 '22 17:10

Ivan Vinogradov