There are several variations on this out there but all are use the OID/FID of the FC to create the query. I'd like to modify this to accommodate a different field which is still an integer value.
The thing is arcpy.Describe makes it damn easy to get a list of FIDs. Am I missing something here or am I going to have to build a list by other means?
I do realize you could use a loop to create a super long query using 'OR' but given that the attribute values are integer, I see no reason to use 'IN' for this.
Here's some code:
import arcpy
#Assumes the layer in ArcMap has a selected features
lyr = arcpy.mapping.Layer("yourLayer")
lyrDesc = arcpy.Describe(lyr)
fieldDesc = lyrDesc.FIDset
selectedList = fieldDesc.split(";")
#Build your query; the below is poor since it assumes "OBJECTID" is present; this is a draft
sql = "{} IN ({})".format("OBJECTID", ", ".join([str(n) for n in selectedList]))
#Apply the definition query
lyr.definitionQuery = sql
I found a real simple way to do this:
#Function to build list
def UniqueValues(table, field):
with arcpy.da.SearchCursor(table, [field]) as cursor:
return sorted({row[0] for row in cursor})
Should I just delete this question all together? I don't want to clutter up the forum.