Skip to main content
Solved

Count unique list elements and add number of occurences to list element

  • March 8, 2021
  • 3 replies
  • 392 views

lambertus
Enthusiast
Forum|alt.badge.img+23

I have a list which contains these values:

nl restservicesMonumenten_raadplegenMapServer   

nl restservicesBR_GroenstructuurplankaartMapServer  

nl restservicesBR_GroenstructuurplankaartMapServer   

nl restservicesBijzondere_BomenFeatureServer   

nl restservicesBR_BAGFeatureServer  

nl restservicesBR_BAGFeatureServer   

nl restservicesVERG_TOEZOPLOSFeatureServer   

nl restservicesVERG_TOEZOPLOSFeatureServer   

nl restservicesVERG_TOEZOPLOSFeatureServer   

nl restservicesVERG_TOEZOPLOSFeatureServer   

nl restservicesPROJ_Mut_editFeatureServer   

nl restservicesPROJ_Mut_editFeatureServer   

nl restservicesPROJ_Mut_editFeatureServer   

nl restservicesPROJ_Mut_editFeatureServer   

nl restservicesPROJ_Mut_editFeatureServer

 

In want to clean up this list by removing the duplicate strings. However, I want to keep the number of times each string occures. Finally, I want to get this list which stores after each string the number of occurence as shown in my first list:

 

nl restservicesMonumenten_raadplegenMapServer  (1)

nl restservicesBR_GroenstructuurplankaartMapServer   (2)

nl restservicesBijzondere_BomenFeatureServer   (1)

nl restservicesBR_BAGFeatureServer   (2)

nl restservicesVERG_TOEZOPLOSFeatureServer (4)

nl restservicesPROJ_Mut_editFeatureServer   (5)

 

Any ideas? I have tried something with an AttributeSplitter folllowed by a ListHistogrammer which leads to two lists. However, I was not able to combine them so far.

Best answer by ebygomm

You could explode the list after the ListHistogrammer, create the new text from the name and the count then rebuild the list

Capture

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • Best Answer
  • March 8, 2021

You could explode the list after the ListHistogrammer, create the new text from the name and the count then rebuild the list

Capture


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • March 8, 2021

Working with lists generally is one area where doing something in python can be more straightforward than using FME. This would take a list called list{} and replace it with a list of the same name but removing the duplicates and adding the counts

import fme
import fmeobjects
from collections import Counter
 
def processFeature(feature):
    #get list item
    mylist = feature.getAttribute('list{}')
    #set count
    count = 0
    #remove original list now stored in mylist
    feature.removeAttribute('list{}')
    #use Counter to count occurrences of value in list and iterate through 
    for k,v in Counter(mylist).items():
        count +=1
        #set newlist attribute
        feature.setAttribute("list{"+str(count)+"}",k + " ("+str(v)+")")

 Capture


lambertus
Enthusiast
Forum|alt.badge.img+23
  • Author
  • Enthusiast
  • 141 replies
  • March 8, 2021

You could explode the list after the ListHistogrammer, create the new text from the name and the count then rebuild the list

Capture

Thanks, works perfect!