Skip to main content
Solved

Search list for values on multiple attributes

  • March 8, 2023
  • 5 replies
  • 178 views

tva
Contributor
Forum|alt.badge.img+12
  • Contributor
  • 45 replies

Hi,

The listsearcher makes it possible to search in a list for a certain value on 1 Attribute.

Is it possible to do this (without listexploder) to search in a list for values on different attributes?

list example:

_names{0}.LC = Arabic

_names{0}.NA = Latin

_names{0}.PN = true

_names{1}.LC = Arabic

_names{1}.NA = Arabic

_names{1}.PN = true

 

I want to search in names list where PN = true & NA <> Latin and get that indexback. In this case it would be 1

If it's not possible in FME, how would I do this via python in FME then?

Best answer by david_r

For Python, do something like:

lc_list = feature.getAttribute('_names{}.LC') or []
na_list = feature.getAttribute('_names{}.NA') or []
pn_list = feature.getAttribute('_names{}.PN') or []
items = zip(lc_list, na_list, pn_list)
 
feature.setAttribute('found_index', -1)
for index, item in enumerate(items):
    if item[2] == 'true' and item[1] != 'Latin':
        feature.setAttribute('found_index', index)
        break

This will return the first found index in the attribute 'found_index'. The value will be -1 if not found. Depending on your testing it may be that you have to modify line 8 to read:

if item[2] == True and item[1] != 'Latin':

 

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, 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.

5 replies

david_r
Celebrity
  • 8391 replies
  • Best Answer
  • March 8, 2023

For Python, do something like:

lc_list = feature.getAttribute('_names{}.LC') or []
na_list = feature.getAttribute('_names{}.NA') or []
pn_list = feature.getAttribute('_names{}.PN') or []
items = zip(lc_list, na_list, pn_list)
 
feature.setAttribute('found_index', -1)
for index, item in enumerate(items):
    if item[2] == 'true' and item[1] != 'Latin':
        feature.setAttribute('found_index', index)
        break

This will return the first found index in the attribute 'found_index'. The value will be -1 if not found. Depending on your testing it may be that you have to modify line 8 to read:

if item[2] == True and item[1] != 'Latin':

 


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3422 replies
  • March 8, 2023

Do you mean PN = true & LC <> Latin ? As this is true for both examples in your list


tva
Contributor
Forum|alt.badge.img+12
  • Author
  • Contributor
  • 45 replies
  • March 8, 2023

Do you mean PN = true & LC <> Latin ? As this is true for both examples in your list

Thanks for pointing this out. Adapted the question


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3422 replies
  • March 8, 2023

Thanks for pointing this out. Adapted the question

Then this line in the code just needs changing

if item[2] == 'true' and item[0] != 'Latin':

to

if item[2] == 'true' and item[1] != 'Latin':

 


david_r
Celebrity
  • 8391 replies
  • March 8, 2023

Thanks for pointing this out. Adapted the question

Thanks, fixed my original answer accordingly.