Tuesday, July 18, 2017

Recipe: Joining two lists and interweaving the terms

Say, in Column 1, you have :
Santa Cruz; Santa Cruz Beach Boardwalk
And in Column 2, you have:
California; Surfing
And you want to join them like so:
Santa Cruz; California; Santa Cruz Beach Boardwalk; Surfing
You can do it using this Jython code (keep the carriage returns): 

first = cells['column1']['value']
split1 = first.split(';')
second = cells['column2']['value']
split2 = second.split(';')
split3 = map('; '.join, zip(split1, split2))
return str.join("; ", split3)

This only works if you have equal amounts of terms in both columns.


No comments: