简体   繁体   English

未检测到 Python 第二个循环

[英]Python second loop not detected

I am using python and some arcpy modules (that I do not think are important for my question) to run a code containing three iterations (a loop with two smaller loops inside).我正在使用 python 和一些 arcpy 模块(我认为这对我的问题并不重要)来运行包含三个迭代的代码(一个内部有两个较小循环的循环)。 My code looks like this:我的代码如下所示:

file = r"C:Directory\File"
cursor = arcpy.SearchCursor(file,fields)
A = range(1,270)
os.chdir ('C:\Directory')
book = xlwt.Workbook()
sheet1 = book.add_sheet('Sheet1',cell_overwrite_ok = True)
for a in A:
tot = 0
result = 0
for row in cursor:
    if row.getValue('Firstfield') == a:
        tot = tot + row.getValue('secondfield')
        print (tot)
    for row in cursor:
        print row.getValue('GTYS_Branc')
        if row.getValue('GTYS_Branc') == a:
            result= result + row.getValue(Field1) * ((row.getValue(Field2))/tot )
    sheet1.write((branch-1),0,finalIIIprob)
    sheet1.write((a-1),1,result)
    book.save('Excel.xls')

The code works but does not give me the results I want.代码有效,但没有给我想要的结果。 I noticed that the problem is that it practically ignores the second internal loop and every iteration of the first internal loop after the first one.我注意到问题在于它实际上忽略了第二个内部循环以及第一个内部循环之后第一个内部循环的每次迭代。 What could the problem be?可能是什么问题?

问题可能是您正在迭代“数组”中的值,但那里只有一个数字 A,而不是从 0 到 A 的所有数字,您需要做的是使用范围函数:尝试我在范围(A):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM