Try the following using an Update Cursor. This assumes 1) your input and output fields are text fields and 2) you have created a new field new_field
in which you will write your results.
import arcpy
fc = r'C:\path\to\your\geodatabase.gdb\featureclass'
with arcpy.da.UpdateCursor(fc, ("dup_field", "new_field")) as cursor:
for row in cursor: # Iterate through each row
if row[0] is not None: # Only perform actions if there is not a None value in row
a = sorted(set(int(x) for x in row[0].split(','))) # Manipulate text field, convert to integer so that
b = str(a).strip('[').strip(']')
row[1] = b # Write the results
cursor.updateRow(row)
The following line converts the text to a list of integer values, which we can then use set()
to isolate unique values. sorted()
obviously sorts the values
a = sorted(set(int(x) for x in row[0].split(',')))
This line cleans up the results of a
by convert to text and removing the brackets so we can write the string back to the attribute table
b = str(a).strip('[').strip(']')
{}
button that enables you to format any highlighted code nicely. Please check out our short tour for more about how the site works. Thanks.