首页 > 解决方案 > 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语句的值。

标签: python

解决方案


可能是类型问题。U 检查浮点 0.0 上的字符串类型 从这里:如果 cl == '0.0':到这里:

    if cl == 0.0:

检查python文档。


推荐阅读