0

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?

0

Found a solution:

layer = parameters[0].valueAsText

mylist = []
rows = arcpy.SearchCursor(layer, fields="surveyor, uniqueID")
for row in rows:
    value = row.getValue("surveyor")
    myid = row.getValue("uniqueID")
    filterexp = '"uniqueID" = ' + str(myid)
    if value not in mylist:
        mylist.append(value)
        arcpy.SelectLayerByAttribute_management(layer, 'ADD_TO_SELECTION', filterexp)
arcpy.AddMessage(mylist)
return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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