print语句正在改变代码行为:python

时间:2014-06-03 18:10:37

标签: python

这是我的代码段

if( start_city == end_city ):
    is_rnd_trip = 1

    for i in range(1,len(keylist)):
        prev_leg = parse(d[keylist[i-1]][5])
        current_leg = parse(d[keylist[i]][4])
        delta = relativedelta(current_leg, prev_leg)
        print
        print delta
        if(delta > max_delta):
            max_delta = delta
            print max_delta
            indx_of_max_delta = i

    end_city = d[keylist[indx_of_max_delta]][1]

print '\nindx_of_max_delta: ',indx_of_max_delta
print start_city,end_city

输出:

relativedelta(hours=+18, minutes=+10)
relativedelta(hours=+18, minutes=+10)

relativedelta(days=+10, hours=+15, minutes=+45)
relativedelta(days=+10, hours=+15, minutes=+45)

relativedelta(hours=+20, minutes=+50)

indx_of_max_delta:  2
SEA BLR

但是,如果我注释掉打印行,我会得到不同的输出。我不知道出了什么问题。我在评论打印行之前得到的输出是正确的,这是我所期望的。我正在使用Spyder编辑器(如果有帮助的话)

if( start_city == end_city ):
    is_rnd_trip = 1

    for i in range(1,len(keylist)):
        prev_leg = parse(d[keylist[i-1]][5])
        current_leg = parse(d[keylist[i]][4])
        delta = relativedelta(current_leg, prev_leg)
        print
        #print delta
        if(delta > max_delta):
            max_delta = delta
            #print max_delta
            indx_of_max_delta = i

    end_city = d[keylist[indx_of_max_delta]][1]

print '\nindx_of_max_delta: ',indx_of_max_delta
print start_city,end_city

输出

indx_of_max_delta:  3
SEA DXB

1 个答案:

答案 0 :(得分:0)

在Python中,迭代器就像一个列表,但它只是一个数据流。一旦它流过你的程序,它就消失了。它非常有效,但您可能会因为发现而感到困惑。

我建议将其转换为列表,这样您就可以将其打印出来并进行处理:

delta = list( relativedelta(current_leg, prev_leg) )