简体   繁体   中英

Iterate the list with float value and then print the same list

I have a list float_list = [0.2, 1.2, 1.5, 0.7, 0.9] with float values.

Where first I am iterating the values of float_list and then multiplying those values form 2 and in the last i want to print the result in a list where desired result must be this [0.4, 2.4, 3.0, 1.4, 1.8]

but i am getting a TypeError: 'float' object is not iterable here is my code

My Code

float_list = [0.2, 1.2, 1.5, 0.7, 0.9]
for i in float_list:
    print([list_num*2 for list_num in i])

Desired Result

[0.4, 2.4, 3.0, 1.4, 1.8]

Error

Traceback (most recent call last):
  File "F:/python-practise/for.py", line 3, in <module>
    print([list_num*2 for list_num in i])
TypeError: 'float' object is not iterable

Note: I've also tried to take i as string str(i) print([list_num*2 for list_num in str(i)]) but in doing so I got this output in my console

['00', '..', '22']
['11', '..', '22']
['11', '..', '55']
['00', '..', '77']
['00', '..', '99']

Now I am just stuck Please help me to get the desired result.

You are mixing loops and list comprehensions. In your loop, i is already a single element of the list, ie a number. You should either just use a loop...

float_list = [0.2, 1.2, 1.5, 0.7, 0.9]
for i in float_list:
    print(i * 2)

... or a list comprehension:

print([i * 2 for i in float_list])
for i in float_list:
    print([list_num*2 for list_num in i])

i will be a float, because floats are in the list. So you can't iterate over a float, because it's not a list. So "for list_num in i will fail.

Just do this:

float_list = [0.2, 1.2, 1.5, 0.7, 0.9]

print([list_num*2 for list_num in float_list])

If you have a simple list (that is not a list of list) , you either use a loop or a list comprehension but not both.

Just delete

for i in float_list:

and you're good.

Note that you have two for loops. You only need one.

>>> float_list = [0.2, 1.2, 1.5, 0.7, 0.9]
>>> new_list = [list_num*2 for list_num in float_list]
>>> print(new_list)
[0.4, 2.4, 3.0, 1.4, 1.8]

I recommend unpacking it when you print:

>>> print(*new_list)
0.4 2.4 3.0 1.4 1.8

Inner list comprehension causes the error because you are applying a for loop over a single float item ie you cant iterate over a float object=>that is what error indicates.

Solution:

use a list comprehension:

>>> float_list
[0.2, 1.2, 1.5, 0.7, 0.9]
>>> print([list_num*2 for list_num in float_list])
[0.4, 2.4, 3.0, 1.4, 1.8]

or a loop like this

>>> new_list=[]
>>> for i in float_list:
...     new_list+=[i*2]
... 
>>> new_list
[0.4, 2.4, 3.0, 1.4, 1.8]
>>> 

I'd recommend mapping a function to each element of list using map() :

>>> float_list
[0.2, 1.2, 1.5, 0.7, 0.9]
>>> list(map(lambda x:x*2, float_list))
[0.4, 2.4, 3.0, 1.4, 1.8]
>>> 

I think this will solve it

result = [2*i for i in float_list]
print(result)

or

print([i*2 for i in float_list])    

Hope it helps. Happy Coding :)

I would handle the task in two steps:

# Declare the variables
float_list = [ ] # your present array
float_double_array = [ ] # your to be new array
  1. Iterate the present list - float_list as below and then multiply by two and then append the result to a float_double_array

     for index_item in float_list: double_item=index_item * 2 float_double_array.append(double_item) 
  2. Now you can use your new float_double_array and it's indexed values as below.

     for double_floats in float_double_array: try: print " ".join(double_floats) #handles python 2 except: print (" ".join(double_floats)) #handles python 3 

Finally, any questions, please ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM