为什么我的 python while 循环不会停止?
Why wont my python while loop stop?
我有一个循环应该 select 功能并继续循环直到它不再 select 使用新功能
arcpy.SelectLayerByLocation_management("antiRivStart","INTERSECT","polygon")
previousselectcount = -1
selectcount = arcpy.GetCount_management("StreamT_StreamO1")
while True:
#selectCount = arcpy.GetCount_management("StreamT_StreamO1")
mylist = []
with arcpy.da.SearchCursor("antiRivStart","ORIG_FID") as mycursor:
for feat in mycursor:
mylist.append(feat[0])
liststring = str(mylist)
queryIn1 = liststring.replace('[','(')
queryIn2 = queryIn1.replace(']',')')
arcpy.SelectLayerByAttribute_management('StreamT_StreamO1',"ADD_TO_SELECTION",'OBJECTID IN '+ queryIn2 )
arcpy.SelectLayerByLocation_management("antiRivStart","INTERSECT","StreamT_StreamO1","","ADD_TO_SELECTION")
previousselectcount = selectcount
selectcount = arcpy.GetCount_management("StreamT_StreamO1")
print str(selectcount), str(previousselectcount)
if selectcount == previousselectcount:
break
根据我的估计,一旦它开始打印姓名编号两次它应该停止,但它没有,它会一遍又一遍地打印“15548 15548”。是忽略中断还是不满足 if 条件?
我也试过
while selectcount != previousselectcount:
但这给了我同样的结果
Python 中的变量是动态的。仅仅因为您将 previousselectcount
初始化为整数并不意味着当您调用 previousselectcount = selectcount
时它将是一个整数。你可以随意摆脱那条线。
如果替换:
selectcount = arcpy.GetCount_management("StreamT_StreamO1")
有:
selectcount = int(arcpy.GetCount_management("StreamT_StreamO1").getOutput(0))
对于这两行,您将比较整数值,而不是相等运算符为对象比较的任何值。
更好的是,为什么不编写一个函数来为您完成:
def GetCount():
return int(arcpy.GetCount_management("StreamT_StreamO1").getOutput(0))
避免重复自己。
我有一个循环应该 select 功能并继续循环直到它不再 select 使用新功能
arcpy.SelectLayerByLocation_management("antiRivStart","INTERSECT","polygon")
previousselectcount = -1
selectcount = arcpy.GetCount_management("StreamT_StreamO1")
while True:
#selectCount = arcpy.GetCount_management("StreamT_StreamO1")
mylist = []
with arcpy.da.SearchCursor("antiRivStart","ORIG_FID") as mycursor:
for feat in mycursor:
mylist.append(feat[0])
liststring = str(mylist)
queryIn1 = liststring.replace('[','(')
queryIn2 = queryIn1.replace(']',')')
arcpy.SelectLayerByAttribute_management('StreamT_StreamO1',"ADD_TO_SELECTION",'OBJECTID IN '+ queryIn2 )
arcpy.SelectLayerByLocation_management("antiRivStart","INTERSECT","StreamT_StreamO1","","ADD_TO_SELECTION")
previousselectcount = selectcount
selectcount = arcpy.GetCount_management("StreamT_StreamO1")
print str(selectcount), str(previousselectcount)
if selectcount == previousselectcount:
break
根据我的估计,一旦它开始打印姓名编号两次它应该停止,但它没有,它会一遍又一遍地打印“15548 15548”。是忽略中断还是不满足 if 条件?
我也试过
while selectcount != previousselectcount:
但这给了我同样的结果
Python 中的变量是动态的。仅仅因为您将 previousselectcount
初始化为整数并不意味着当您调用 previousselectcount = selectcount
时它将是一个整数。你可以随意摆脱那条线。
如果替换:
selectcount = arcpy.GetCount_management("StreamT_StreamO1")
有:
selectcount = int(arcpy.GetCount_management("StreamT_StreamO1").getOutput(0))
对于这两行,您将比较整数值,而不是相等运算符为对象比较的任何值。
更好的是,为什么不编写一个函数来为您完成:
def GetCount():
return int(arcpy.GetCount_management("StreamT_StreamO1").getOutput(0))
避免重复自己。