How do I retrieve the lowers and uppers bounds in a discontinuous list of values (alphanumeric)?
Example of list: {5, 6, 7, 8, 10a, 11, 15, 16x, 17, 18, 19, 20, 25}
The extraction I'm looking for would be: 5-8, 10a-11, 15-20, 25
Thank you
How do I retrieve the lowers and uppers bounds in a discontinuous list of values (alphanumeric)?
Example of list: {5, 6, 7, 8, 10a, 11, 15, 16x, 17, 18, 19, 20, 25}
The extraction I'm looking for would be: 5-8, 10a-11, 15-20, 25
Thank you
I'm sure this can be done using transformers, but I suspect it'll be rather convoluted. Here's a possible solution using a PythonCaller:
import fmeobjects
from itertools import groupby
from operator import itemgetter
def str2int(s):
return int(''.join([x for x in s if x.isdigit()]))
def split_list_at_bounds(feature):
data = feature.getAttribute('values')
if data:
data = [x.strip() for x in data.replace('{', '').replace('}', '').split(',')]
bounds = []
for k, g in groupby(enumerate(data), lambda (i,x):i-str2int(x)):
bounds.append(map(itemgetter(1), g))
for n, boundary in enumerate(bounds):
if len(boundary) == 1:
s = boundary[0]
else:
s = '-'.join([boundary[0], boundary[-1]])
feature.setAttribute('boundary{%s}' % n, s)
Assuming the input attribute values = "{5, 6, 7, 8, 10a, 11, 15, 16x, 17, 18, 19, 20, 25}" it will output list boundary{} like the following:
The grouping mechanism in str2int() will strip away all non-digits, so be careful if you have values like 12x3 as it will be interpreted as 123 and not 12.
Yep, convoluted!
I am admiring the flexibility of this tool and the genius of its users.
Thank you
I need a little time to test all these solutions. Thank you
I need a little time to test all these solutions. Thank you
I need a little time to test all these solutions. Thank you
Enter your username or e-mail address. We'll send you an e-mail with instructions to reset your password.