I look for lists comparaison. For example : i have 2 lists list1 that contains (1,2,3 values) and list2 that contains (2,3,4,5).
The result is 2 lists ListOut1 and ListOut2 that contain the differences :
ListOut1 will contains all list2 values that don't exist in list1 => ListOut1 values : 4,5
ListOut2 will contains all list1 values that don't exist in list2=> ListOut2 values : 1.
I dont find tranfermer that can helps me. I look for python script but i need help.
Thanks an advance.
FarFar
Best answer by takashi
Hi,
David, fmeobjects.FMEFeature.getAttribute returns "None" if specified attribute does not exist, will not throw exception. I don't think try - except clause is preferable to handle empty inputs. And, both getAttribute and setAttribute can treat a list attribute directly (except nested list). I would write this script in this case. ----- import fmeobjects def findDiff(feature): list1 = feature.getAttribute('list1{}.values') if list1 == None: list1 = [] list2 = feature.getAttribute('list2{}.values') if list2 == None: list2 = [] feature.setAttribute('diff1{}.values', list(set(list2) - set(list1))) feature.setAttribute('diff2{}.values', list(set(list1) - set(list2))) ----- In addition, using "set" structure is efficient to find differed elements between two sets, but the order of resultant elements might change (uncontrollable). If the order of elements has to be preserved, an iterative processing will be necessary. For example: ----- feature.setAttribute('diff1{}.values', [a for a in list2 if a not in set(list1)]) feature.setAttribute('diff2{}.values', [a for a in list1 if a not in set(list2)]) -----
Did this help you find an answer to your question?
This post is closed to further activity.
It may be a question with a best answer, an implemented idea, or just a post needing no comment.
If you have a follow-up or related question, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.
This assumes that each feature has at least one or more elements in "list1{}.values" and "list2{}.values". The difference will be output in "diff{}.values", which you will have to expose in the PythonCaller to make it visible in the workbench.
Sorry, was a bit quick to reply, didn't get the part about two output lists. Here's a more correct answer that also handles the case where either of the input lists are empty:
I created a short test scenario. I created two lists like yours.
I used then the ListExploder to get all the features. In the next step I took the ChangeDetector. Here you can choose what is the original list and what should be compared. As Parameters I set Match Geometry = None. Moreover I choosed an Attribute wher my list elemets are stored (->Selected attributes). The Added Output port gives the elements that are not in the list.
David, fmeobjects.FMEFeature.getAttribute returns "None" if specified attribute does not exist, will not throw exception. I don't think try - except clause is preferable to handle empty inputs. And, both getAttribute and setAttribute can treat a list attribute directly (except nested list). I would write this script in this case. ----- import fmeobjects def findDiff(feature): list1 = feature.getAttribute('list1{}.values') if list1 == None: list1 = [] list2 = feature.getAttribute('list2{}.values') if list2 == None: list2 = [] feature.setAttribute('diff1{}.values', list(set(list2) - set(list1))) feature.setAttribute('diff2{}.values', list(set(list1) - set(list2))) ----- In addition, using "set" structure is efficient to find differed elements between two sets, but the order of resultant elements might change (uncontrollable). If the order of elements has to be preserved, an iterative processing will be necessary. For example: ----- feature.setAttribute('diff1{}.values', [a for a in list2 if a not in set(list1)]) feature.setAttribute('diff2{}.values', [a for a in list1 if a not in set(list2)]) -----
thanks, you're of course right about getAttribute() returning None rather than an Exception object when the list doesn't exist.
Regarding the order, I'd say that depends on this specific case, whether it is important. Python sets have the advantage of being very fast and the resulting code is very easy to read, so I tend to prefer that solution unless order absolutely need to be preserved.
If the order is numerical, it is also easy to re-order it either using a ListSorter on the resulting list, or by using list1.sort() / list2.sort() somewhere in the Python script.
Regarding the order, I agree with you. There must be cases that the ListSorter or "sort" is suitable. All depends on the requirement and actual data condition.
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.