Skip to main content

Example:

I've got a list: "_list" with as attributes _list{}.ratio.  

  • _list{0}.ratio = 0.10
  • _list{1}.ratio = 0.25
  • _list{2}.ratio = 0.30

And I want to add a list attribute: _list{}.ratiosum with as result:

  • _list{0}.ratiosum = 0.10
  • _list{1}.ratiosum = 0.35
  • _list{1}.ratiosum = 0.65

In Python I would do it like this: 

sumlist = t] 
s = 0 
for i in range(len(ratio)): 
s = s + ratiooi] 
sumlist.append(s) 

But how is the relation between the python and the list in FME? This doesn't work:

def processFeature(feature):
    s = 0
    for i in range(len(feature.getAttribute{'_list{}'));
        s = s + ratioÂi]
        feature.setAttribute('_list{%d}.ratiosum' % i, s)      

This gives an syntax error; it is not possible to get the length of the list with the len() function?

 

 

 

Hi @erikjh, "feature.getAttribute('_list{}.ratio')" returns a list containing the elements as string values. For example:

    sum = 0.0
    ratios = feature.getAttribute('_list{}.ratio')
    for i in range(len(ratios)):
        sum += float(ratiosbi])
        feature.setAttribute('_list{%d}.ratiosum' % i, sum)

or

    sum = 0.0
    for i, r in enumerate(feature.getAttribute('_list{}.ratio')):
        sum += float(r)
        feature.setAttribute('_list{%d}.ratiosum' % i, sum)

and so on.


Hi @erikjh

you can also try a no-Python option:

  • ListExploder to explode the list into individual features;
  • StatisticsCalculator > Cumulative to get ratiosum values;
  • Aggregator to combine multiple features back into a single feature with a list attribute.

Lena


Thank you very much! This will help me a lot.


Reply