Skip to main content
Solved

Loop through list with python

  • September 12, 2016
  • 9 replies
  • 416 views

Forum|alt.badge.img

How can I access list elements dynamically and put it in a loop? I tried this:

index = 2
r = @Value(keys_list{index}.key)

but understand the 'at' is just a templating syntax and cant be used like this..

Also, if there is any specific list functions or a dedicated list object, I have not been able to find them under  Class FMEFeature in the documentation.

Thanks,

UPDATE:

Tried

        keys_list = feature.getAttribute('keys_list{}')
        for key in keys_list:
            pass        
        self.pyoutput(feature)

This gives me Python Exception <TypeError>: 'NoneType' object is not iterable

Verified: keys_list exists and has several elements.

Best answer by jeroenstiers

my_list = feature.getAttribute('attribute_list{}.key')
for item in my_list:    
	print item

 

Hi @giskis 

You have to specify which element of the list you would like to load. 

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

9 replies

david_r
Celebrity
  • September 12, 2016

Try something like

my_list = feature.getAttribute('attribute_list{}')
for item in my_list:
    print item

This will read the FME list "attribute_list{}" into the Python list "my_list", then iterate and print each element in that list.

If you have a complex list, it would look like this:

my_list = feature.getAttribute('attribute_list{}.item_name')
for item in my_list:
    print item

Forum|alt.badge.img
  • Author
  • September 12, 2016
david_r wrote:

Try something like

my_list = feature.getAttribute('attribute_list{}')
for item in my_list:
    print item

This will read the FME list "attribute_list{}" into the Python list "my_list", then iterate and print each element in that list.

If you have a complex list, it would look like this:

my_list = feature.getAttribute('attribute_list{}.item_name')
for item in my_list:
    print item
Thanks, still cant get it to work though, pls check update in question.

 

 


Forum|alt.badge.img+7
  • Best Answer
  • September 12, 2016
my_list = feature.getAttribute('attribute_list{}.key')
for item in my_list:    
	print item

 

Hi @giskis 

You have to specify which element of the list you would like to load. 


takashi
Influencer
  • September 12, 2016

The error occurs when the specified list doesn't exist. If 'keys_list' is the base name of a complex list (i.e. a list consisting of one or more components such as keys_list{}.key), you will have to qualify the list name with a component name. e.g. 'keys_list{}.key'.


david_r
Celebrity
  • September 12, 2016

Try:

keys_list = feature.getAttribute('keys_list{}.key')
if keys_list:
    for key in keys_list:
        print key

Forum|alt.badge.img
  • Author
  • September 12, 2016
jeroenstiers wrote:
my_list = feature.getAttribute('attribute_list{}.key')
for item in my_list:    
	print item

 

Hi @giskis 

You have to specify which element of the list you would like to load. 

Thanks, nailed it!

 

 


Forum|alt.badge.img
  • April 11, 2018

Hello,

I have a set of point and a grid partition. each point is assign to a partition

I have build a list containing all point in a partition (listbuilder group by partition_id)

Is there a way to get the feature geometry of each element of the list.

Does the geometry is preserved in a listbuilder ?

if so How can I retreive it within the list of element in python?

so far I only get NoneType in my_list.

0684Q00000ArJ3uQAF.png

def input(self, feature):

    my_list = feature.getAttribute("_list{}")

    # flush the list
    self.fList = []

    if my_list is not None and len(my_list) > 0:
        for item in my_list:
            new_feature = feature.clone()
            self.fList.append(new_feature)

        self.DBScan(self.fList, self.eps, self.minPts)
    else:
        partition = feature.getAttribute("partition")
        self.logger.logMessageString("partition {} as no value : ".format(partition) , fmeobjects.FME_ERROR)

Thank

Sylvain


Forum|alt.badge.img
  • April 11, 2018
david_r wrote:

Try:

keys_list = feature.getAttribute('keys_list{}.key')
if keys_list:
    for key in keys_list:
        print key

Hello,

I have a set of point and a grid partition. each point is assign to a partition

I have build a list containing all point in a partition (listbuilder group by partition_id)

Is there a way to get the feature geometry of each element of the list.

Does the geometry is preserved in a listbuilder ?

if so How can I retreive it within the list of element in python?

so far I only get NoneType in my_list.

0684Q00000ArMHWQA3.png

def input(self, feature):

    my_list = feature.getAttribute("_list{}")

    # flush the list
    self.fList = []

    if my_list is not None and len(my_list) > 0:
        for item in my_list:
            new_feature = feature.clone()
            self.fList.append(new_feature)

        self.DBScan(self.fList, self.eps, self.minPts)
    else:
        partition = feature.getAttribute("partition")
        self.logger.logMessageString("partition {} as no value : ".format(partition) , fmeobjects.FME_ERROR)

Thank

Sylvain


Forum|alt.badge.img
  • April 11, 2018
david_r wrote:

Try:

keys_list = feature.getAttribute('keys_list{}.key')
if keys_list:
    for key in keys_list:
        print key

Hello,

I have a set of point and a grid partition. each point is assign to a partition

I have build a list containing all point in a partition (listbuilder group by partition_id)

Is there a way to get the feature geometry of each element of the list.

Does the geometry is preserved in a listbuilder ?

if so How can I retreive it within the list of element in python?

so far I only get NoneType in my_list.

0684Q00000ArM8OQAV.png

def input(self, feature):

    my_list = feature.getAttribute("_list{}")

    # flush the list
    self.fList = []

    if my_list is not None and len(my_list) > 0:
        for item in my_list:
            new_feature = feature.clone()
            self.fList.append(new_feature)

        self.DBScan(self.fList, self.eps, self.minPts)
    else:
        partition = feature.getAttribute("partition")
        self.logger.logMessageString("partition {} as no value : ".format(partition) , fmeobjects.FME_ERROR)

Thank

Sylvain


Reply


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