如何解决 TypeError: 'float' object is not iterable

新手上路,请多包涵

我怎样才能转移

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

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

我尝试了下面的代码,但发生了错误:

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

TypeError: 'float' object is not iterable

谁能帮帮我?

原文由 FlyingBurger 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.4k
2 个回答

tl;博士

尝试 list comprehension ,它更方便:

 new = [[i] for i in A]


解释

您得到 TypeError 因为您不能将 list() 函数应用于 float 类型的值。 此函数 将可迭代对象作为参数,并且 float 不是可迭代对象。

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作为参数添加的项目。

原文由 Ivan Vinogradov 发布,翻译遵循 CC BY-SA 4.0 许可协议

你有一个错误,尝试:

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

这是更漂亮的解决方案:

 new = [[i] for i in A]

原文由 stasdeep 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题