I want to make a code that select all unique values of a field in a attribute table. However, while everything in the code apparently works, I don't know how to add the current row in the Search Cursor in my selection.
layer = parameters[0].valueAsText
mylist = []
rows = arcpy.SearchCursor(layer, fields="Surveyor")
for row in rows:
value = row.getValue("Surveyor")
if value not in mylist:
mylist.append(value)
arcpy.SelectLayerByAttribute_management(layer, 'ADD_TO_SELECTION')
arcpy.AddMessage(mylist)
return
On the message (second last line), the program prints a list with 3 unique values. Yet, the code always select every single row. Makes sense, since there was no filter on my SelectLayer, so it just select the whole layer. So I tried using a field with Unique Values:
layer = parameters[0].valueAsText
mylist = []
rows = arcpy.SearchCursor(layer, fields="Surveyor, uniqueID")
for row in rows:
value = row.getValue("Surveyor")
myid = row.getValue("uniqueID")
if value not in mylist:
mylist.append(value)
arcpy.SelectLayerByAttribute_management(layer, 'ADD_TO_SELECTION', '"uniqueID" = myid')
arcpy.AddMessage(mylist)
return
But no success, it still selects the whole layer. So, how I make the SelectLayerbyAttribute to add to the selection only the row that the cursor is located?
arcpy.SearchCursor
: Usearcpy.da.SearchCursor
instead.