Skip to main content
Solved

Help with a Python script on a list


Forum|alt.badge.img

Hello,

 

I have a list called ''list'' of variable length depending on features.

 

I wanna assign as an attribute to each feature ''list(0) ; list(1) .... list(lenght(list))''

 

I tried writing a loop on the attribute manager but I didn't know how to use it well

 

Thanks for any possible help :)

Best answer by david_r

Try something like this:

import fmeobjects

VALUE_DELIMITER = ','
ITEM_DELIMITER = ';'

def concat_list(feature):
    distances = feature.getAttribute('_list{}._distance')
    orders = feature.getAttribute('_list{}._order')
    types = feature.getAttribute('_list{}._DZ_Type')
    items = []
    for values in zip(distances, orders, types):
        items.append(VALUE_DELIMITER.join(values))
    feature.setAttribute('result', ITEM_DELIMITER.join(items))

Play around with VALUE_DELIMITER and ITEM_DELIMITER until you get what you need.

Given the input lists:

0684Q00000ArKDTQA3.png

The result will be:

0684Q00000ArKJQQA3.png

View original
Did this help you find an answer to your question?

9 replies

takashi
Evangelist
  • May 28, 2018

Do you want to concatenate all element values in a list?

For example, if the list stores these values,

List ElementValue_list{0}._distance10_list{1}._distance20_list{2}._distance30 do you need to create this attribute? Attribute NameValue_distances10;20;30

david_r
Celebrity
  • May 28, 2018

I'm not quite sure I understand what you want to do with the list, but have you looked at the ListExploder? It will create one feature for each list item, depending on the list length.

If you simply need a string with all the concatenated list values, have a look at the ListConcatenator.


Forum|alt.badge.img
takashi wrote:

Do you want to concatenate all element values in a list?

For example, if the list stores these values,

List ElementValue_list{0}._distance10_list{1}._distance20_list{2}._distance30 do you need to create this attribute? Attribute NameValue_distances10;20;30
Thank you for your reply,

 

What I wanna do is : ''_list(0)._distance" + ''_list(0)._order" + ''_list(0)._DZ_Type" + ''_list(1)._distance" + ''_list(1)._order" + ''_list(1)._DZ_Type"

 

and so on until the end of the list.

 

The list have a different length depending on each feature

 


Forum|alt.badge.img
david_r wrote:

I'm not quite sure I understand what you want to do with the list, but have you looked at the ListExploder? It will create one feature for each list item, depending on the list length.

If you simply need a string with all the concatenated list values, have a look at the ListConcatenator.

Hello,

 

I have read its documentation but I think it's not the appropriate one what I wanna do is :

 

''_list(0)._distance" + ''_list(0)._order" + ''_list(0)._DZ_Type" + ''_list(1)._distance" + ''_list(1)._order" + ''_list(1)._DZ_Type"

 

and so on until the end of the list.

 

The list have a different length depending on each feature

 


takashi
Evangelist
  • May 29, 2018
takashi wrote:

Do you want to concatenate all element values in a list?

For example, if the list stores these values,

List ElementValue_list{0}._distance10_list{1}._distance20_list{2}._distance30 do you need to create this attribute? Attribute NameValue_distances10;20;30
I'm still unclear what your requirement is. Could you please explain the conditions and requirement more specifically using source and destination tables, like the one I have posted.

 

 


david_r
Celebrity
  • Best Answer
  • May 29, 2018

Try something like this:

import fmeobjects

VALUE_DELIMITER = ','
ITEM_DELIMITER = ';'

def concat_list(feature):
    distances = feature.getAttribute('_list{}._distance')
    orders = feature.getAttribute('_list{}._order')
    types = feature.getAttribute('_list{}._DZ_Type')
    items = []
    for values in zip(distances, orders, types):
        items.append(VALUE_DELIMITER.join(values))
    feature.setAttribute('result', ITEM_DELIMITER.join(items))

Play around with VALUE_DELIMITER and ITEM_DELIMITER until you get what you need.

Given the input lists:

0684Q00000ArKDTQA3.png

The result will be:

0684Q00000ArKJQQA3.png


Forum|alt.badge.img
david_r wrote:

Try something like this:

import fmeobjects

VALUE_DELIMITER = ','
ITEM_DELIMITER = ';'

def concat_list(feature):
    distances = feature.getAttribute('_list{}._distance')
    orders = feature.getAttribute('_list{}._order')
    types = feature.getAttribute('_list{}._DZ_Type')
    items = []
    for values in zip(distances, orders, types):
        items.append(VALUE_DELIMITER.join(values))
    feature.setAttribute('result', ITEM_DELIMITER.join(items))

Play around with VALUE_DELIMITER and ITEM_DELIMITER until you get what you need.

Given the input lists:

0684Q00000ArKDTQA3.png

The result will be:

0684Q00000ArKJQQA3.png

Thank you very much

 

It worked

 

 


Forum|alt.badge.img
takashi wrote:
I'm still unclear what your requirement is. Could you please explain the conditions and requirement more specifically using source and destination tables, like the one I have posted.

 

 

I am sorry if my request was somehow unclear

 

I tried the python script above and it worked

 

I managed also meanwhile to complete the task using a combination of transformers

 

thank you very much

 

 


takashi
Evangelist
  • May 29, 2018
takashi wrote:
I'm still unclear what your requirement is. Could you please explain the conditions and requirement more specifically using source and destination tables, like the one I have posted.

 

 

Good to hear that you got a solution.

 

By the way, how the list attribute was created in your workflow? Depending on the condition, there could be a more simple solution without scripting.

 

For example, the list has been created with the Generate List option in a transformer, you can create a new attribute concatenating the three attribute values (_distance, _order, and _DZ_Zone) before creating the list, then just concatenate elements in the resulting list, with the ListConcatenator following to the transformer.

 

 


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