Question

deleting the first element of a list


Hello!

I'm new to FME and I'm getting stupid about a simple task:

I want to take the second element of list I created and write it into a new attribute (X2), which doesn't work.

If I begin by grabbing the first element like this, it works:

import fmeobjects
class FeatureProcessor(object):
def input(self, feature):
feature.setAttribute("X2",feature.getAttribute('List{0}.X'))
self.pyoutput(feature)

However, If I change that code to start grabbing values by the second row (change the code to "feature.getAttribute('List{1}.X')", I get an empty output.

Can anyone help with this? Or is there any other solution that fits? Basically, I have an X-Coordinate-Attribute and I want that X-Coordinate to be substracted with the following X-Coordinate in the same attribute. I thought deleting the first element and then merging the resulting attribute to the original attribute would be the easiest solution.

 

Thank you!


3 replies

Userlevel 1
Badge +21

Is there a reason for using python?

If you want to get the second element of a list as an attribute you can do this, or use a listindexer to do the same

Userlevel 4

I agree with @ebygomm, try to keep it simple whenever possible.

Also, are you 100% sure that the list has more than 1 element when it arrives to the PythonCaller? Try setting a breakpoint before the PythonCaller and inspect the list elements.

Userlevel 4
Badge +25

As to the Python code, I've tried this and it does seem to work fine for me.

I do echo the other comments here though:

  • Is there a reason you need to use Python? It's generally easier to use a transformer
  • Does your list always have an entry for {1}? You can check in the Inspector, even on the PythonCaller output

One other thought - which is perhaps more a question for the Python community - is there a reason to use a Class rather than a Function? I understood Functions are for processing single features, which you are doing here.

In fact, that makes me wonder if you are possibly misunderstanding the nature of a list, which is a set of attribute values on a single feature, not a sequence of features.

i.e. if you want to replace the X value of one record with the X value of the next, then try Adjacent Feature Attributes in the AttributeManager (we can help with that if it's the case).

In general we don't have a list element deleter/remover. What I've done in the past is add a Counter transformer before I create the list so that I also have list{x}.ID. If I want to ignore the first record I set list{0}.ID to 9999 and then use a ListSorter on ID, so that the top element is now at the bottom.

It's not the same as deleting, but it often does what I need.

I hope something here is of use.

Reply