[英]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. 我有一个带有float值的列表float_list = [0.2, 1.2, 1.5, 0.7, 0.9]
。
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]
首先我要迭代float_list的值,然后将这些值乘以 2 ,最后我要将结果打印到列表中,期望结果必须是[0.4, 2.4, 3.0, 1.4, 1.8]
0.4,2.4,3.0,1.4,1.8 [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 但是我收到一个TypeError: 'float' object is not iterable
这是我的代码
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 注意:我也尝试将i作为字符串str(i) print([list_num*2 for list_num in str(i)])
str(i)中 print([list_num*2 for list_num in str(i)])
但是这样做是在控制台中得到的
['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. 在您的循环中, i
已经是列表的单个元素,即一个数字。 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. 因此,“对于我中的list_num将失败。
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. 请注意,您有两个for
循环。 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. 内部列表理解会导致错误,因为您在单个float
项上应用了for循环,即您无法在float
对象上进行迭代=>这就是错误所指示的内容。
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()
: 我建议使用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
Iterate the present list - float_list as below and then multiply by two and then append the result to a float_double_array 迭代当前列表-如下所示的float_list,然后乘以2,然后将结果附加到float_double_array
for index_item in float_list: double_item=index_item * 2 float_double_array.append(double_item)
Now you can use your new float_double_array and it's indexed values as below. 现在,您可以使用新的float_double_array及其索引值,如下所示。
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. 最后,如有任何疑问,请询问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.