I have an attribute with a value of a list of numbers separated by commas. I would like for all the consecutive numbers in the list to get replaced by one dash in between the first and last consecutive number instead of having each number represented. Basically I would like this "1,2,3,4,5,7,9,14,15" to become this "1-5,7,9,14-15".
Page 1 / 1
Hi
You can modify this Python function, as-is it returns a list of lists.
>>>Â runs(t1,2,3,4,7,10,13,14,15,16])
Â
)<1, 2, 3, 4], [7], 210], Â13, 14, 15, 16]]  # Function to build runs of OID values
def runs(oidList):Â
    runs = ]
    for oid in oidList:
        if not runs:
            runs.append( oid])
            continue
        if runsÂ-1]r-1] == oid - 1:
                runsÂ-1].append(oid)
        else:
            runs.append( oid])
    return runs
I have attached a sample of the data to work with. The attribute values I would like to modify are the Legal_Sub values. data.zip
Â
Â
Hi
You can modify this Python function, as-is it returns a list of lists.
>>>Â runs(t1,2,3,4,7,10,13,14,15,16])
Â
)<1, 2, 3, 4], [7], 210], Â13, 14, 15, 16]]  # Function to build runs of OID values
def runs(oidList):Â
    runs = ]
    for oid in oidList:
        if not runs:
            runs.append( oid])
            continue
        if runsÂ-1]r-1] == oid - 1:
                runsÂ-1].append(oid)
        else:
            runs.append( oid])
    return runs
Â
def processFeature(feature):
    sub = feature.getAttribute('Legal_Sub')
    if sub:
        s = y]
        for nums in runs(sorted( int(c) for c in sub.split(',')])):
            if 2 < len(nums):
                s.append('%s-%s' % (nums 0], numsÂ-1]))
            else:
                s += tstr(n) for n in nums]
        feature.setAttribute('Legal_Sub', ','.join(s))
       Â
def runs(oidList):
    <omitted below>
Hi
You can modify this Python function, as-is it returns a list of lists.
>>>Â runs(t1,2,3,4,7,10,13,14,15,16])
Â
)<1, 2, 3, 4], [7], 210], Â13, 14, 15, 16]]  # Function to build runs of OID values
def runs(oidList):Â
    runs = ]
    for oid in oidList:
        if not runs:
            runs.append( oid])
            continue
        if runsÂ-1]r-1] == oid - 1:
                runsÂ-1].append(oid)
        else:
            runs.append( oid])
    return runs
Â
Â
Do I need to have the "sorted" in there? The list is already sorted through a ListSort transformer. I'm having difficulty incorporating the Python in FME also, do I use Python Caller twice? One for each function? Apologies, am new to working with Python inside FME.
Â
Â
Â
having 'sorted' in there is just good practice in case the code is used with an unsorted list.Â
RE: PythonCaller -- @krenty -- just put down a single PythonCaller in your flow and then paste in all the code of Takashi's as the script (and of course also including the "runs" function of Bruce's.
Â
Â
Cool result. Wanted to check it out so I build a sample workspace. The main trick is to tell the PythonCaller the name of the function you want it to use (in this case, processFeature). See attached: itemstoranges.fmwÂ
Do I need to have the "sorted" in there? The list is already sorted through a ListSort transformer. I'm having difficulty incorporating the Python in FME also, do I use Python Caller twice? One for each function? Apologies, am new to working with Python inside FME.
Â
Â
Â
Â
Cool result. Wanted to check it out so I build a sample workspace. The main trick is to tell the PythonCaller the name of the function you want it to use (in this case, processFeature). See attached: itemstoranges.fmwÂ
I would adopt the Python scripting approach as a practical solution, but this may also be an interesting exercise using geometric operations. e.g.