python - For循环没有正确写入列表?
问题描述
我知道这是基本的东西,我确定我一定只是误解了这里的循环或错过了一个小技巧,但为什么这段代码不起作用?这个问题对我来说是看不见的!
print(itemclientprice_clean)
for pu, cl in zip(itempurchaseprice_clean, itemclientprice_clean):
if cl == '0.0':
profit_list.append('0')
else:
profit = pu - cl
profit_list.append(profit)
print(profit_list)
输出:
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
[129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0, 129000.0]
第二个列表应该都是0?然而,它是else
语句的值。
解决方案
可能是类型问题。U 检查浮点 0.0 上的字符串类型 从这里:如果 cl == '0.0':到这里:
if cl == 0.0:
检查python文档。
推荐阅读
- terraform - Terraform 属性“子网”的值不合适:元素 0:需要字符串
- javascript - React-Leaflet 离线存储瓦片
- html - 如何使用 linux 工具下载输入到代码路径中的文本?
- android - 如何检查应用程序是否在后台运行?
- linux - 如何在 Shell 脚本中编写程序
- neo4j - 如何使用 ngrok 公开在 localhost 上运行的 neo4j 实例以供 google colab 使用?
- go - 集成 Go 和 Bazel 测试
- c++ - Visual Studio 专业版 2017 链接器
- deep-learning - 我们在哪里将标签传递给 CNN 图像分类器?
- python - Django - ValueError:套接字尚未被接受,因此无法通过它发送