我有一个循环,它应该选择特征并一直循环直到它不再选择新特征
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))
避免重复自己。
