Skip to main content
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

ebygomm
Influencer
Forum|alt.badge.img+32
  • Influencer
  • May 28, 2020

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


david_r
Celebrity
  • May 28, 2020

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.


mark2atsafe
Safer
Forum|alt.badge.img+44

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.


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