-3

谁能告诉我为什么这个循环忽略了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)
4

2 回答 2

2

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

于 2013-03-20T22:56:39.890 回答
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
于 2013-03-20T23:09:10.917 回答