I'm trying to provide the breakdown of fire detection for an area by sorting of confidence levels. The dataset has 12 files (one for each month in a year) with fire records which all have a column called "confidence" with a value between 0 and 100. I have to use a for loop to query the files by 3 different confidence levels. Group 1: >85% confidence Group 2: 60-85% confidence Group 3: <60% confidence

I HAVE TO USE a for loop to execute this. Any ideas? I started by appending all the files into one list. I'm told that I could use lists to set up lower and upper ranges and SelectLayerByAttribute_management, and use that in a loop, but don't know how to do that.

This is my final code that works. I decided to merge the files together into one big file.

arcpy.env.workspace = "***********/data/" 

list1 = []; 

for month in range(1,13): 
    inf = "2007" + str(month).zfill(2)+ "_rfe" 
    arcpy.DefineProjection_management(newpath + inf + ".shp", 4326)
    list1.append(newpath + inf + ".shp") 

out1 = "big2007" + "_rfe" + ".shp" 
layer1 = "layer1" + ".shp" 
arcpy.MakeFeatureLayer_management(out1, layer1)

lower = [0, 60, 86] 
upper = [60, 86, 101] 

for i in range(len(lower)):
    arcpy.SelectLayerByAttribute_management(layer1, "NEW_SELECTION", """ "CONF" >= {} and "CONF" < {}""".format(lower[i],upper[i])) 
    result = arcpy.GetCount_management(layer1).getOutput(0) 
    result = int(round(result)) 

1 Answer 1


I assume that your files are in the same directory. I don't know why you Have to use a for loop, as it adds unnecessary complexity, but here is an example where you can create two lists (one with the lower ranges and the second with the upper ranges), that you insert into the where clause when creating the layer (you can also create a single layer without where clause, and use the same where clause to select by attribute within this layer)

minvals = [85, 60 , -1  ]
maxvals = [100, 85, 60]

fcs = arcpy.ListFeatureClasses()
for fc in fcs:
    for i in range(len(minvals)):
        arcpy.MakeFeatureLayer(fc, fc[:-4]+str(i+1), """ "confidence_field_name" > {} And "confidence_field_name" <= {}""".format(minvals[i],maxvals[i]) )
  • Yeah, the files are in the same directory, but I had to change their projection before doing anything, which you can see in my edit. I have to use a for loop because I'm trying to learn looping with simple tasks first. For my edit, I reversed the order of the lists, and made a couple of other edits, but still end up with error for the "MakeFeatureLayer" line.
    – cesium133
    Apr 9, 2015 at 9:10
  • as currently written, your feature class list will probably be empty. try "print fcs" to check what's in it. Two reasons are :1) workspace is not specified and 2) wildcard string needs a "*". Also, not that your identation should be corrected, but this is probably only a layout issue.
    – radouxju
    Apr 9, 2015 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.