试图理解为什么只在第一次调用嵌套 for 循环

Trying to understand why a nested for loop is called only the first time

我有以下代码:

def get_preds(train,test,x_ranges,y_ranges):
    global total_scores
    global num_scores
    for x_min, x_max in x_ranges:
       for y_min, y_max in y_ranges:
           ...

当我在 python 2.7 上 运行 此方法时,它的行为符合预期。嵌套 for 循环 运行s 每次外层 for 循环 运行s.

当我将相同的代码加载到 Kaggle python 脚本引擎上时 运行s 在 Python 3 上,嵌套循环仅 运行s 一次。对于所有其他迭代,它被跳过。

方法的调用方式如下:

dataset = pd.read_csv('../input/train.csv',dtype=types,index_col=0)
split_t=math.floor((0.9)*786239)

train = dataset[dataset.time < split_t]
test = dataset[dataset.time >= split_t]


def gen_ranges(size,step):
    return zip(np.arange(0,size,step), np.arange(step, size+step, step));

x_ranges = gen_ranges(size,x_step)
y_ranges = gen_ranges(size,y_step)

preds_test_total = get_preds(train,test,x_ranges,y_ranges)

在Python3中,zip()returns一个迭代器。您只能迭代一次迭代器的元素。要复制 Python 2 行为,请使用 list(zip(...)) 代替 zip(...)