Python代码忽略if语句

2024-10-04 05:23:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在编写应用程序的过程中,我遇到了一个奇怪的python代码片段,我无法理解:

代码:

msg = [108878, [[314.06, 2, 4.744018], [314.03, 1, 15.9059], [314.02, 2, 79.8338531], [314, 1, 54.90047253], [313.56, 2, 1.75392219], [313.53, 2, 15.61132219], [313.5, 1, 0.554316], [313.42, 1, 1.5976], [313.27, 1, 0.43344], [313.26, 1, 62.724], [313.25, 1, 2.57518855], [313.24, 1, 0.04], [313.09, 2, 22.51784808], [312.9, 1, 40], [312.82, 1, 26.65592034], [312.7, 1, 35.53791008], [312.62, 1, 0.46912], [312.61, 1, 100], [312.6, 1, 48.33502123], [312.57, 1, 4.24547326], [312.56, 2, 0.2], [312.5, 2, 109.76863639], [312.43, 1, 100], [312.42, 1, 0.11142352], [312.4, 1, 7.815571], [314.09, 2, -3.01187461], [314.14, 1, -1.27056771], [314.39, 2, -9.31898324], [314.46, 1, -0.01930229], [314.49, 1, -0.40344], [314.5, 1, -3.40637161], [314.53, 1, -0.2], [314.54, 2, -0.46432889], [314.57, 1, -0.04200538], [314.71, 1, -0.050949], [314.84, 1, -0.02153813], [314.88, 1, -0.04200538], [314.93, 1, -68.439], [314.94, 2, -7.477782], [314.95, 1, -5], [315.1, 1, -5.97], [315.12, 1, -0.01], [315.16, 1, -40], [315.2, 1, -0.04200538], [315.22, 1, -25.7525], [315.23, 1, -78.54523718], [315.38, 1, -80], [315.42, 1, -6.65060488], [315.47, 1, -20], [315.48, 1, -5.36]]]

bids = asks = {}
lenbids = lenasks = 0
for order in msg[1]:
    if float(order[2])>0:
        lenbids +=1
        bids[order[0]]=order[2]
    elif float(order[2])<0:
        lenasks+=1
        asks[order[0]]=-order[2]

print(len(bids),len(asks),lenbids,lenasks)

输出:

50 50 25 25

在我看来,python在lenbids/lenasks操作方面的行为是正确的,但是无论条件是否满足,它都在执行if语句的第二部分

如果这有什么区别的话,我会和Python3一起玩Pycharm

非常感谢您的帮助


Tags: 代码in应用程序forlenif过程order