1

I have duplicate elements in one field (11223334) and would like them to bu unique in another (1234). I made a new field and am trying to use update cursor:

>>> with arcpy.da.UpdateCursor("SD_Pattern_CLSCOM",["Day_Pattern","Day_Pattern2"]) as cursor:
...     for row in cursor:
...         a = set()
...         for i in row[0]:
...             a.add(i)
...         row[1] = a
...         cursor.updateRow(row)

...
And I am getting this error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 7, in <module>
TypeError: value #1 - unsupported type:
  • 1
    What type of field is Day_Pattern and Day_Pattern2? You are trying to store the set itself into the row, not the contents. Do you need the values to stay in the same order? I believe python sets do not retain value order, so you could end up with set([4,2,1,3]) – Midavalo May 25 '17 at 1:57
2

As Midavalo mentioned, I'm going to assume both fields are text fields. You can eliminate some of your lines of code and replace it with something similar.

with arcpy.da.UpdateCursor("SD_Pattern_CLSCOM",["Day_Pattern","Day_Pattern2"]) as cursor:
    for row in cursor:
        row[1] = ''.join(set(row[0]))
        cursor.updateRow(row)

Of course, python sets don't necessarily retain the original sorting. So, a string of '22113334' might return '1324'.

You'll have to do a bit more processing to keep the original order, if necessary.

  • Thank you Fezter. I added sorted in front of set and got what I needed. – geoJshaun May 25 '17 at 17:39
  • @ShaunO Please note that sorted will put the values in numeric order, not keep it in the original order, so Fezter's example of 22113334 would be returned as 1234 and not 2134 – Midavalo May 26 '17 at 2:41
1

You are trying to store a set like set([1,2,3,4]) in your field, not a value 1234. Also I think sets don't retain value order, so could be set([4,2,1,3]).

Try this instead:

If fields are Text fields:

import itertools

with arcpy.da.UpdateCursor("SD_Pattern_CLSCOM",["Day_Pattern", "Day_Pattern2"]) as cursor:
    for row in cursor:
        x = ''.join(xx for xx, _ in itertools.groupby(row[0]))
        row[1] = x
        cursor.updateRow(row)

If fields are Integer fields:

import itertools

with arcpy.da.UpdateCursor("SD_Pattern_CLSCOM",["Day_Pattern", "Day_Pattern2"]) as cursor:
    for row in cursor:
        x = int(''.join(xx for xx, _ in itertools.groupby(str(row[0]))))
        row[1] = x
        cursor.updateRow(row)

Hat-tip

Note this won't remove additional instances of the same number, like in 1122331155 it will return 12315.

  • 1
    My two cents here, if the order is important and the values should be unique, x=[];[x.append(i) for i in str(row[0]) if i not in x] will do. – fatih_dur May 25 '17 at 7:19
  • Thank you Midavalo. I went with Fezter's answer because it was more in the realm of my understanding, not to say your solution isn't just as correct. – geoJshaun May 25 '17 at 17:41

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.