gpt4 book ai didi

Python 嵌套 for-in 循环 - 最外层循环不迭代

转载 作者:行者123 更新时间:2023-11-30 23:05:19 25 4
gpt4 key购买 nike

简单的问题,不确定出了什么问题,但是:我正在尝试迭代从 csv 文件读取的两个列表,如下所示:

for row1 in (list(csv_data1)):
for row2 in (list(csv_data2)):
# do something with row2 and row2

然而,每次迭代外层for循环后,内层for循环都无法识别外层for循环被迭代了!例如,如果我这样做:

for row1 in (list(csv_data1)):
for row2 in (list(csv_data2)):
# do something with row2 and row2
print row1

row1 的元素被正确打印。但是,如果我尝试在内部循环中打印最外层循环的元素,如下所示:

for row1 in (list(csv_data1)):
for row2 in (list(csv_data2)):
# do something with row2 and row2
print row1

我多次只获得(list(csv_data1))的第一行!

因此,如果 csv_data1 = [['a','b'],['b','c']] 例如,我期望上面的打印语句(在内部循环中打印)打印:

[['a','b']
# repeated prints of above for however long csv_data2 is ...
['b','c']]
# repeated prints of above for however long csv_data2 is ...

但我得到以下信息:

[['a','b']
# repeated prints of above for however long csv_data2 is ...
['a','b']]
# repeated prints of above for however long csv_data2 is ...

即我无法让两个循环相互迭代。我错过了一些非常明显的东西,任何帮助将不胜感激。谢谢。

编辑:更具体地说,这是我正在尝试做的事情:(我现在只是打印以尝试诊断问题)

f1 = open('file1.csv', 'rU')
f2 = open('file2.csv', 'rU')
reader1 = csv.DictReader(f1)
reader2 = csv.DictReader(f2)

# Grab desired columns from csv file
cols_desired = 'district,blockname,villagename'.split(',')

desired_cols_1 = (list(row[col]) for col in cols_desired) for row in reader1)
desired_cols_2 = (list(row[col]) for col in cols_desired) for row in reader2)

for row1 in (list(desired_cols_1)):
for row2 in (list(desired_cols_2)):
print row1
# XXX this prints only the first row of list(desired_cols_1) repeated times for some reason!

最佳答案

问题是您正在为内部循环使用生成器。一旦您迭代生成器一次,生成器就为空。因此,在第一个循环中,您消耗了 csv_data2 的所有元素,然后对于以下所有循环来说它都是空的。

看看这个:

>>> x = (i for i in range(5))
>>> y = (i for i in range(5))
>>> for i in x:
... ylist = list(y)
... print(id(ylist))
... print(len(ylist))
...
44917584
5
44917624
0
44918104
0
44918144
0
44918184
0
>>> print(len(list(x)))
0

每次迭代都会创建一个新列表,并且在除第一次迭代之外的所有迭代中,ylist 都是空的。这是因为第一次迭代在创建列表消耗了生成器的元素。 x 上也有类似的效果:在 for 循环之后它也是空的。这就是您所看到的。

解决方案是在循环之前创建列表:

# Square brackets make this a list comprehension instead of a raw generator
# List comprehension gives back a list
desired_cols_1 = [list(row[col]) for col in cols_desired) for row in reader1]
desired_cols_2 = [list(row[col]) for col in cols_desired) for row in reader2]

for row1 in desired_cols_1:
for row2 in desired_cols_2:
print row1, row2

这只会消耗发电机一次。

或者,如果数据太大,您无法将其全部加载到内存中,您可以为每次迭代创建一个新的生成器,而不是在循环之前创建内部生成器:

desired_cols_1 = (list(row[col]) for col in cols_desired) for row in reader1)

for row1 in desired_cols_1:
# Need to make sure the reader is back at the beginning
reader2.seek(0)
desired_cols_2 = (list(row[col]) for col in cols_desired) for row in reader2)
for row2 in desired_cols_2:
print row1, row2

关于Python 嵌套 for-in 循环 - 最外层循环不迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33205021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com