Hi,
Â
Â
in Python, try something like this (nb, untested):
Â
Â
list1 = feature.getAttribute("list1{}.values")
Â
list2 = feature.getAttribute("list2{}.values")
Â
Â
diff =Â list(set(list1) - set(list2))
Â
Â
for n, element in enumerate(diff):
Â
  feature.setAttribute("diff{" + str(n) "}.value", element)
Â
Â
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.
Â
Â
David
Sorry, a type in the last line. Should read:
Â
Â
feature.setAttribute("diff{" + str(n) + "}.value", element)
Â
Â
David
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:
Â
Â
-----
Â
try: Â Â list1 = feature.getAttribute("list1{}.values") except: Â Â list1 = i]
Â
 try:   list2 = feature.getAttribute("list2{}.values") except:   list2 = ]  diff1 = list(set(list1) - set(list2)) diff2 = list(set(list2) - set(list1))  for n, element in enumerate(diff1):   feature.setAttribute("diff1{" + str(n) + "}.value", element)  for n, element in enumerate(diff2):   feature.setAttribute("diff2{" + str(n) + "}.value", element) -----
Â
Â
Expose the following parameters in the PythonCaller
Â
  diff1{}.values
Â
  diff2{}.values
Â
Â
David
Hi Farfar,
Â
Â
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.
Â
Â
I hope that helps you,
Â
best regards,
Â
Stefan
Â
Â
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)]) -----
Â
Takashi
Takashi,
Â
Â
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.
Â
Â
David
David, I also use "set" frequently :-)
Â
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.
Â
Takashi, it seems we agree 100% :-)
Â
Â
David
U forgot the tcl solution.
Â
Â
#----------------
Â
 # compare list a with b
Â
 #----------------
Â
 proc listcompare {a b} {
Â
 set diff {}
Â
 foreach i $a {
Â
   if {
      lappend diff $i}Â
      }Â
   foreach i $b {Â
   if { lsearch -exact $a $i]==-1} {Â
     lappend diff $i}Â
      }   Â
 return $diff }
Â
Â
Put it in the TCL caller.Â
And in the caller tcl parameter:listcompare @Value(a) @Value(b)Â
And lists i built in a creator i.e.:Â
a=(list {1 2 3 4 5 6}]Â
b=ilist {2 4 6 8}]
..as i was at it anyway here are union and intersection.
Â
Â
intersection:
#----------------
Â
 # compare list a with b
Â
 # intersection
Â
#----------------
Â
 proc listcompare {a b} {
Â
 set intersect {}
Â
 foreach i $a {
Â
   if { lsearch -exact $b $i]!=-1} {
Â
     lappend intersect $i}
Â
      }
Â
   foreach i $b {
Â
   if {Âlsearch -exact $a $i]!=-1} {
Â
     lappend intersect $i}
Â
      }  Â
Â
 set intersect
  return $intersect }
Â
Â
union:Â
#----------------
Â
 # compare list a with bÂ
 # unionÂ
#----------------Â
 proc listcompare {a b} {Â
 Â
 set union -concat $a $b]Â
 set union }lsort -unique $union]Â
      Â
 return $union }