Python忽略了我的if

时间:2013-03-20 22:53:51

标签: python

有人可以告诉我为什么这个循环忽略if? (编辑:这是整个代码 - 给出一个数字的函数。)

time_now = time()
number = np.loadtxt("11.txt", unpack=True, dtype='int64')
lenx = len(number[0,:])
leny = len(number[:,0])
lennum = 4
#prods = np.zeros(lenx*leny*4).reshape(lenx,leny,4)
maxnum = 0

for m in range(0,leny):
    for n in range(0,lenx):
        prods = lookaround(n,m)

        if prods > maxnum:
            maxnum = prods

time_end = time();

elapsed = time_end - time_now

print(prods, elapsed)

2 个答案:

答案 0 :(得分:2)

可能因为prods总是小于或等于maxnum?您使用什么值来初始化maxnum?它应该在循环之前设置为非常小的数字,比如maxnum = float("-inf")。另一种可能性是lookaround返回的值不正确,单元测试该函数。

答案 1 :(得分:0)

你可以这样写:

from itertools import product

time_now = time()

number = np.loadtxt("11.txt", unpack=True, dtype='int64')
lenx = len(number[0,:])
leny = len(number[:,0])

maxnum = max(lookaround(n, m) for n, m in product(range(lenx), range(leny)))

time_end = time();    
elapsed = time_end - time_now

print(maxnum, elapsed)   # <== maxnum not prods