0

I have developed a python script to copy and paste in the field calculator. Having an input of three values, it compares all of them and returns a list of unique values. The problem is that when I use it in ArcMap, it only returns the first result for all the rows, even they should be different. Then, how can I evaluate each row?

Here is the code:

index =  0 
list_yes = [] 
def myFunc(var_1,var_2,var_3):

        global list_yes
        global index


    myList_1 = [var_1,var_2,var_3]
    for i in myList_1:
        myList_2 = myList_1[index:]
        for j in myList_2:
            if i == j:
                if not j:
                    pass
                else:
                    list_yes.append(j)
            elif not j:
                pass
            else:
                pass

        index = index +1

    list_set = set(list_yes)
    unique_list = list(list_set)
    return str(unique_list)
  • 1
    If you are looking for a list of unique values, try list(set(myList_1)) – Bjorn Apr 1 at 14:11
  • I know, but it is Python 3, isn't it? ArcMap uses Python 2.7 – Ric1002 Apr 1 at 15:09
  • 2
    list(set(myList_1)) will work in Python 2.7. – Bjorn Apr 1 at 15:13
  • why do you want the str of the list? – Paul H Apr 1 at 18:39
  • Paul , if I convert it to str, I can process it later in FME – Ric1002 Apr 3 at 14:03
1

It is unclear if you are trying to populate a new field with the unique values from three others or if you only want to list all unique values from three columns.

To print all unique values in three fields use the set data type and da.SearchCursor (also see How to make a flat list out of list of lists?):

fc = "somefeatureclass" #Change
fields = ["field1","field2","field3"] #Change
print set([item for sublist in arcpy.da.SearchCursor(fc,fields) for item in sublist])

To calculate a new field with the unique values from three other fields on the same row use the da.UpdateCursor:

fc = "somefeatureclass"
fields = ["field1","field2","field3", "field_to_update"]

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        row[-1] = ','.join([str(a) for a in set(row[:-1])]) #Create a comma separated string with unique values
        cursor.updateRow(row)
  • Thank you for this reply, I haven't tested it yet, I should do it. Then, I will tell you! – Ric1002 Apr 3 at 14:05

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.