How to count the amount of times a value is present

Other approaches to this question. It's also a good example of "poor cat" :)
> Community: How to count the amount of times a value is present

1. ListHistogrammer Edition *see the Note below
1) Aggregator
    Attributes to Concatenate: UPDATE
    Separator Character: ; (semicolon)
2) AttributeSplitter
    Attribute to Split: UPDATE
    Delimiter or Format String: ; (semicolon)
    Trim Whitespace: Both
    List Name: _list
    Drop Empty Parts: Yes
3) ListHistogrammer
    Source List Attribute: _list{}
    Histogram List Name: _histogram
4) ListExploder
    List Attribute: _histogram{}
Note: Currently there is a bug on the ListHistogrammer in FME 2014. Don't use it until it's fixed.
FME 2013 is OK.

2. PythonCaller Edition
# Python Script Example
import fmeobjects

class UpdateCounter(object):
    def __init__(self):
        self.counter = {}
    def input(self, feature):
        update = feature.getAttribute('UPDATE')
        if update:
            for v in [s.strip() for s in str(update).split(';')]:
                self.counter[v] = self.counter.setdefault(v, 0) + 1
    def close(self):
        for v in self.counter.keys():
            feature = fmeobjects.FMEFeature()
            feature.setAttribute('value', v)
            feature.setAttribute('count', self.counter[v])

No comments:

Post a Comment