Select to view content in your preferred language

How to iterate through a list to select by attribute?

5484
2
Jump to solution
09-16-2019 01:50 PM
RyanHowell1
Occasional Contributor

I am trying to iterate through a field (integer values 1, 2, 3), select by each unique attribute, and run a tool on that selection of points.

My list is:

NearValues = [1, 2, 3]

And my for loop is the following (again, I want to select all of the points with a value of 1, run the median center tool on all of those points, then select all of the 2's, etc):

for value in NearValues:
    query = "['nearstr']="+str(value)
    arcpy.SelectLayerByAttribute_management(points, "NEW_SELECTION", query)
    arcpy.MedianCenter_stats(points, "mediancenter_test" + str(value))‍‍‍‍

It's giving me an Invalid expression error, so I'm not sure if it's a minor syntax thing or if there's something wrong with my methodology. Here is the complete error if it's helpful:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 7605, in SelectLayerByAttribute
    raise e
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 7602, in SelectLayerByAttribute
    retval = convertArcObjectToPythonObject(gp.SelectLayerByAttribute_management(*gp_fixargs((in_layer_or_view, selection_type, where_clause, invert_where_clause), True)))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 498, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).

‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor

first, I think you need the query correct for the database the points dataset is in.

currently, for value = 1, the query reports back:  "['nearstr']=1" which doesn't look right.

make sure the query is working first, then move on to the stats.

it appears as if the MedianCenter_stats is not populated correctly either.

MedianCenter_stats (Input_Feature_Class, Output_Feature_Class, {Weight_Field}, {Case_Field}, Attribute_Field)


vs

arcpy.MedianCenter_stats(points, "mediancenter_test" + str(value))
‍‍‍‍‍
‍‍‍‍‍‍‍

you have an input FC, a name for the output, but no path with it and appending a string of value to it??.

I think you are trying to append the value number to the end of the filename.

there should be the numeric attribute field that you want the stats performed on.

also, I'd just use a feature layer as my input as it honors the selection and are easy to work with in this type of case.

The following is working for me on SQL SDE:

arcpy.env.overwriteOutput = True

OutPath = r'C:\Users\xxx\Desktop\tmp\working.gdb'
AttributeField = "ATT_FIELD"
NearValues = [1,2,3]
points_layer = "Points_Layer"
 
for value in NearValues:
	OutFile = OutPath + os.sep + "mediancenter_test_" + str(value)
	if arcpy.Exists(OutFile):
		arcpy.management.Delete(OutFile)
	query = "nearstr = " + str(value)
	arcpy.MakeFeatureLayer_management(points, "Points_Layer", query)
	arcpy.MedianCenter_stats("Points_Layer", OutFile, AttributeField)
	arcpy.management.Delete(points_layer)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

2 Replies
RhettZufelt
MVP Notable Contributor

first, I think you need the query correct for the database the points dataset is in.

currently, for value = 1, the query reports back:  "['nearstr']=1" which doesn't look right.

make sure the query is working first, then move on to the stats.

it appears as if the MedianCenter_stats is not populated correctly either.

MedianCenter_stats (Input_Feature_Class, Output_Feature_Class, {Weight_Field}, {Case_Field}, Attribute_Field)


vs

arcpy.MedianCenter_stats(points, "mediancenter_test" + str(value))
‍‍‍‍‍
‍‍‍‍‍‍‍

you have an input FC, a name for the output, but no path with it and appending a string of value to it??.

I think you are trying to append the value number to the end of the filename.

there should be the numeric attribute field that you want the stats performed on.

also, I'd just use a feature layer as my input as it honors the selection and are easy to work with in this type of case.

The following is working for me on SQL SDE:

arcpy.env.overwriteOutput = True

OutPath = r'C:\Users\xxx\Desktop\tmp\working.gdb'
AttributeField = "ATT_FIELD"
NearValues = [1,2,3]
points_layer = "Points_Layer"
 
for value in NearValues:
	OutFile = OutPath + os.sep + "mediancenter_test_" + str(value)
	if arcpy.Exists(OutFile):
		arcpy.management.Delete(OutFile)
	query = "nearstr = " + str(value)
	arcpy.MakeFeatureLayer_management(points, "Points_Layer", query)
	arcpy.MedianCenter_stats("Points_Layer", OutFile, AttributeField)
	arcpy.management.Delete(points_layer)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
RyanHowell1
Occasional Contributor

Thanks for your input. Making a new feature layer as my input as you described solved my problem.

0 Kudos