Python List和If语句问题

问题描述 投票:0回答:1

我已经创建了一个名为listExitPrice的列表,里面有值。我希望将这个列表分离成多个其他列表,如下图所示。我想通过查看什么listExitPrice[k]来分离它,并检查它是否大于0,如果大于0,我想按照其他的路径进行。反之亦然。这段代码得出的结果是 "list indices must be integer or slices, not tuples",为 else 语句的第一行。任何帮助将被感激! 谢谢!我有一个名为listExit的列表,它的名字叫 "listExit"。

countSell = 0
countHold = 0
for k in listExitPrice:
    if k == 0:
        listBuyDateAP[countHold] = listBuyDate[k]
        listStockNameAP[countHold] = listStockName[k]
        listEntryPriceAP[countHold] = listEntryPrice[k]
        listVolumeAP[countHold] = listVolume[k]
        countHold = countHold + 1
    else:
        listBuyDatePT[countSell] = listBuyDate[k]
        listStockNamePT[countSell] = listStockName[k]
        listEntryPricePT[countSell] = listExitPrice[k]
        listVolumePT[countSell] = listVolume[k]
        listExitPricePT[countSell] = listEntryPrice[k]
        listSellDatePT[countSell] = listSellDate[k]
        countSell = countSell + 1
python list if-statement
1个回答
0
投票

在不知道你的K值是什么样的情况下,我只能根据错误猜测它不是一个整数(可以用来索引数组),而是一个元组。你可能想打印出listExitPrice自己看看。

© www.soinside.com 2019 - 2024. All rights reserved.