-1

我正在循环几个嵌套循环。如果它发生变化,我想打印第一个循环的项目。我怎样才能做到这一点?

for slope in slopeList:
    for yarddist in yardDistList:
                      for chiptreesperacre in chipAcreList:
                          for chipvolpertree in chipVolList:
                              for smalllognumber in smallAcreList:
                                   for smalltreevolpertree in smallVolList:
                                       for largelogperacre in largeAcreList:
                                           for largetreevolpertree in largeVolList:
                                               data = [slope, yarddist, chiptreesperacre, chipvolpertree, smalllognumber, smalltreevolpertree, largelogperacre, largetreevolpertree]
                                               if slope changes:
                                                   print data
4

2 回答 2

3

存储您之前看到的值,并比较它们:

previous_slope = None
for slope in as_many_loops_as_you_like:
    data = [slope, other_stuff]
    if slope != previous_slope:
        print data
        previous_slope = slope
于 2013-04-30T21:08:24.520 回答
0

您可以将 previous_slope 添加到一个集合中,并在内部循环中添加后观察它的长度:

slope = set()
slope.add(previous_slope)
old_length = len(slope)
for first_loop:
   slope.add(current_slope)
   if len(slope) > old_length: 
     print 'Changed!'
     break

对每个内部循环重复此模式。

于 2013-04-30T22:02:23.243 回答