Python - 从嵌套循环中只打印一次
Python - Printing only once from a nested loop
我有一个 excel 文件,如下所示:
col1 col2 col3 col4
-----------------------------
row1 | 2,3,1 _ 1 w
row2 | 3,2,7 _ 2 x
row3 | _ _ 3 y
row4 | 4,9 _ 4 z
我在第 2 列中写了一些值(使用 XLWT),如下所示:
col1 col2 col3 col4
-----------------------------
row1 | 2,3,1 x,y,w 1 w
row2 | 3,2,7 y,x 2 x
row3 | _ _ 3 y
row4 | 4,9 z 4 z
我的问题是我的代码在正确的位置迭代正确的值,但是次数太多(我认为这是嵌套循环的结果)。
这是代码中最重要的部分(绒毛已被剪掉)。
def question():
newtprint = set() #set I created outside of loops to call later one, supposed to prevent repeated iterations from nested loops
for x in xrange(sheet.nrows): #iterates over all rows
for i in xrange(len(newton)): #col1
for n in xrange(len(james)): #col2
if newton[i] == james[n]:
newtprint.add(path[n]) #adds specified col4 value into set
print newtprint, x #writes current set being written and row it's written into
sheety.write(x, 1, str(newtprint)) #writes into col2 based on col4 sets
wb.save('C:/.../names.xls')
我之前在这方面得到了帮助,我认为它有效,但我想它并没有像我最初认为的那样起作用:
只是在寻找防止不断迭代的选项。这是我的 cmd 中出现的内容:
红色括号显示第一组值正在正常打印,然后它开始在一小段时间内一遍又一遍地打印这些相同的值,然后转到下一组值并重复打印这些值。
这样更有意义:
def question():
for x in xrange(sheet.nrows): #iterates over all rows
newtprint = set()
for i in xrange(len(newton)): #col1
for n in xrange(len(james)): #col2
if newton[i] == james[n]:
newtprint.add(path[n]) #adds specified col4 value into set
print newtprint, x #writes current set being written and row it's written into
sheety.write(x, 1, str(newtprint)) #writes into col2 based on col4 sets
wb.save('C:/.../names.xls')
您将 newtprint
的每一行写入新文件。因此,您应该为每一行创建一个新集合。
我有一个 excel 文件,如下所示:
col1 col2 col3 col4
-----------------------------
row1 | 2,3,1 _ 1 w
row2 | 3,2,7 _ 2 x
row3 | _ _ 3 y
row4 | 4,9 _ 4 z
我在第 2 列中写了一些值(使用 XLWT),如下所示:
col1 col2 col3 col4
-----------------------------
row1 | 2,3,1 x,y,w 1 w
row2 | 3,2,7 y,x 2 x
row3 | _ _ 3 y
row4 | 4,9 z 4 z
我的问题是我的代码在正确的位置迭代正确的值,但是次数太多(我认为这是嵌套循环的结果)。
这是代码中最重要的部分(绒毛已被剪掉)。
def question():
newtprint = set() #set I created outside of loops to call later one, supposed to prevent repeated iterations from nested loops
for x in xrange(sheet.nrows): #iterates over all rows
for i in xrange(len(newton)): #col1
for n in xrange(len(james)): #col2
if newton[i] == james[n]:
newtprint.add(path[n]) #adds specified col4 value into set
print newtprint, x #writes current set being written and row it's written into
sheety.write(x, 1, str(newtprint)) #writes into col2 based on col4 sets
wb.save('C:/.../names.xls')
我之前在这方面得到了帮助,我认为它有效,但我想它并没有像我最初认为的那样起作用:
只是在寻找防止不断迭代的选项。这是我的 cmd 中出现的内容:
红色括号显示第一组值正在正常打印,然后它开始在一小段时间内一遍又一遍地打印这些相同的值,然后转到下一组值并重复打印这些值。
这样更有意义:
def question():
for x in xrange(sheet.nrows): #iterates over all rows
newtprint = set()
for i in xrange(len(newton)): #col1
for n in xrange(len(james)): #col2
if newton[i] == james[n]:
newtprint.add(path[n]) #adds specified col4 value into set
print newtprint, x #writes current set being written and row it's written into
sheety.write(x, 1, str(newtprint)) #writes into col2 based on col4 sets
wb.save('C:/.../names.xls')
您将 newtprint
的每一行写入新文件。因此,您应该为每一行创建一个新集合。