Question

Failed to create unique list Python


I have written a Python code to retrieve a list of  unique values from several rows with several attributes (in the example below, there are 5 attributes).

 

The problem with this code is that the output field has only the result for the first row. It copies it to the rest of the fields. Here is the code:

 

 

import fme
import fmeobjects
index =  0
list_yes = []
# Template Function interface:
# When using this function, make sure its name is set as the value of
# the 'Class or Function to Process Features' transformer parameter
def compare_attributes(feature):
    global list_yes
    global index
    
    s1=feature.getAttribute('Att_1')
    s2=feature.getAttribute('Att_2')
    s3=feature.getAttribute('Att_3')
    s4=feature.getAttribute('Att_4')
    s5=feature.getAttribute('Att_5')
    myList_1 = [s1,s2,s3,s4,s5]
    for i in myList_1:
        myList_2 = myList_1[index:]
        if i!='':
            for j in myList_2:
                if i == j:
                    list_yes.append(i)
                else:
                    pass
        index = index +1
    list_set = set(list_yes)
    unique_list = list(list_set)
    var_def = str(unique_list)
    feature.setAttribute('NEWATT',var_def)

 

I have been looking for another built-in Python function and I found the following :

 

 

list(set(feature)

 

 

so i implemented it in the code like this:

 

 

 

import fme
import fmeobjects
# Template Function interface:
# When using this function, make sure its name is set as the value of
# the 'Class or Function to Process Features' transformer parameter
def compare_attributes(feature):
    s1=feature.getAttribute('Att_1')
    s2=feature.getAttribute('Att_2')
    s3=feature.getAttribute('Att_3')
    s4=feature.getAttribute('Att_4')
    s5=feature.getAttribute('Att_5')
    myList_1 = [s1,s2,s3,s4,s5]
    unique_list = list(set(myList_1))
    feature.setAttribute('NEWATT',unique_list)


This code doesn't work, it shows the following error:

 

 

2019-04-01 17:50:54|   0.5|  0.0|ERROR |Python Exception <TypeError>: Failure to convert list unicode values to native UTF-8 values.

2019-04-01 17:50:54|   0.5|  0.0|ERROR |Error encountered while calling function `compare_attributes'

2019-04-01 17:50:54|   0.5|  0.0|FATAL |PythonCaller_31(PythonFactory): PythonFactory failed to process feature

2019-04-01 17:50:54|   0.5|  0.0|ERROR |A fatal error has occurred. Check the logfile above for details

 

 

Is it possible to correct this? 

 

 


2 replies

Userlevel 2
Badge +12

Do you really need to do this in Python?

The DuplicateFilter transformer is built for this task.

Badge +22

You are using the function interface which processes each feature independently. If you want a unique set of values across all features, you need to use the class interface.

Reply