Skip to main content
Solved

Search nested list elements

  • September 23, 2015
  • 6 replies
  • 88 views

Hi,

 

 

How is it possible to search nested lists in FME? By nested list, I mean that list that is returned from SpatialRelator and contains pass criterias.

Best answer by jdh

If you want to do complex list manipulation without exploding into multiple features, then python is a good choice.

 

 

If you just want to know if _relationships{}.pass{}  contains a specific value than something like the following should work:

def processFeature(feature):

 

    ct = feature.getAttribute('_related_candidates')

 

    value = feature.getAttribute('_criteria')

 

    i = 0

 

    while i <2:

 

        p = feature.getAttribute('_relationships{'+str(i)+'}.pass{}')

 

        i+=1

 

        if value in p:

 

            feature.setAttribute('_found', 'Yes')

 

        else:

 

            feature.setAttribute('_found', 'No')

 

 

where _criteria contains the value your looking for. 

 

 

The trick with lists and python is you can retrieve a list, but not a matrix,  so you can't simply get _relationships{}.pass{}  but you can retrierve _relationships{0}.pass{} or _relationships{}.pass{0}  so with nested lists you need to to wrap the getAttribute in a loop
View original
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.

6 replies

david_r
Celebrity
  • September 23, 2015
Hi

 

 

The easiest is probably to use two ListExploders, the first for "_relationships{}" and the next for "pass{}". You can then use e.g. a Tester on "pass" to check the results.

 

 

David

  • Author
  • September 23, 2015
I figured it out, but wanted to avodi ListExploder. Thanks anyway.

jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • Best Answer
  • September 23, 2015

If you want to do complex list manipulation without exploding into multiple features, then python is a good choice.

 

 

If you just want to know if _relationships{}.pass{}  contains a specific value than something like the following should work:

def processFeature(feature):

 

    ct = feature.getAttribute('_related_candidates')

 

    value = feature.getAttribute('_criteria')

 

    i = 0

 

    while i <2:

 

        p = feature.getAttribute('_relationships{'+str(i)+'}.pass{}')

 

        i+=1

 

        if value in p:

 

            feature.setAttribute('_found', 'Yes')

 

        else:

 

            feature.setAttribute('_found', 'No')

 

 

where _criteria contains the value your looking for. 

 

 

The trick with lists and python is you can retrieve a list, but not a matrix,  so you can't simply get _relationships{}.pass{}  but you can retrierve _relationships{0}.pass{} or _relationships{}.pass{0}  so with nested lists you need to to wrap the getAttribute in a loop

david_r
Celebrity
  • September 23, 2015
Hi

 

 

I agree, I try to avoid the ListExploder as much as possible too, it has a tendency to make the memory consumption explode.

 

 

Depending on what you're trying to accomplish, it can sometimes be much faster to implement your list iteration in in a PythonCaller, that way you avoid the overhead of creating a new feature from each list element.

 

 

David

 

 

gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • September 23, 2015
i simply use a tester (or attribute creator) for that.

 

You can acces those lists even without exposing them.

 

 

Or you can also create a comma separated string of the results and use a ïn"clause in your tester.

 

If the number is unknonw use a listelementcounter or so.

 

Or loop etc.etc.

 

 

Ther is no need at all for reverting to python or tcl (or java).

 

 

No need to exlode the list, even when using transformers and no scripting.

 

 

 

jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • September 24, 2015

Changed my mind, the logic should be:

def processFeature(feature):

 

    ct = feature.getAttribute('_related_candidates')

 

    value = feature.getAttribute('_criteria')

 

    i = 0

 

    found = 'No'

 

    while i <2:

 

        p = feature.getAttribute('_relationships{'+str(i)+'}.pass{}')

 

        i+=1

 

        if value in p:

 

            found = 'Yes'

 

    feature.setAttribute('_found', found)

 

 

the original version would only tell you the yes/no of the last item in the list, also reduced the overhead of setting the attribute at each iteration of the loop.

 

            

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings