I am not sure if there's an easy itertools.groupby
solution, but here is an iterative approach that should work:
def mySplit(iterable, sep):
output = []
sepcount = 0
current_output = []
for i, elem in enumerate(iterable):
if elem != sep:
sepcount = 0
current_output.append(elem)
if (i==(len(iterable)-1)):
output.append(current_output)
else:
if current_output:
output.append(current_output)
current_output = []
sepcount+=1
if (i==0) or (sepcount > 1):
output.append([])
if (i==(len(iterable)-1)):
output.append([])
return output
Testing on your examples:
testLists = [
['a', 'SEP', 'b', 'c', 'SEP', 'SEP', 'd'],
["a", "SEP", "SEP", "SEP"],
["SEP"],
["a", "b", "c"]
]
for tl in testLists:
print(mySplit(tl, sep="SEP"))
#[['a'], ['b', 'c'], [], ['d']]
#[['a'], [], [], []]
#[[], []]
#[['a', 'b', 'c']]
This is analogous to the result you would get if examples were actually strings and you used str.split(sep)
:
for tl in testLists:
print("".join(tl).split("SEP"))
#['a', 'bc', '', 'd']
#['a', '', '', '']
#['', '']
#['abc']
By the way, if the elements in your lists were always guaranteed to be strings, you could simply do:
for tl in testLists:
print([list(x) for x in "".join(tl).split("SEP")])
#[['a'], ['b', 'c'], [], ['d']]
#[['a'], [], [], []]
#[[], []]
#[['a', 'b', 'c']]
But the mySplit()
function is more general.