Try this:
def group_by_sep(items, sep='|'):
inner_list = []
for item in items:
if item == sep:
yield inner_list
inner_list = []
else:
inner_list.append(item)
if inner_list:
yield inner_list
Data=['Label',23,'NORM','|','RESP',1.256,None,'|','','','|','RELV','','','|','|','now','|']
SubList = list(group_by_sep(Data, '|'))
print(SubList)
# [['Label', 23, 'NORM'], ['RESP', 1.256, None], ['', ''], ['RELV', '', ''], [], ['now']]
Note that a itertools.groupby
approach can be used here, but it is not equivalent to the above and offers less control over the exact behavior:
import itertools
def group_by_sep2(items, sep='|'):
yield from (
list(g)
for k, g in itertools.groupby(items, key=lambda x: x == sep)
if not k)
SubList2 = list(group_by_sep2(Data, '|'))
print(SubList2)
# [['Label', 23, 'NORM'], ['RESP', 1.256, None], ['', ''], ['RELV', '', ''], ['now']]
It is missing the empty list
between two consecutive separators.
Additionally, it is not as efficient as the direct method from above:
%timeit list(group_by_sep(Data))
# 1000 loops, best of 3: 1.47 µs per loop
%timeit list(group_by_sep2(Data))
# 100 loops, best of 3: 4.01 µs per loop
%timeit list(group_by_sep(Data * 1000))
# 1000 loops, best of 3: 1.33 ms per loop
%timeit list(group_by_sep2(Data * 1000))
# 100 loops, best of 3: 2.83 ms per loop
%timeit list(group_by_sep(Data * 1000000))
# 1000 loops, best of 3: 1.67 s per loop
%timeit list(group_by_sep2(Data * 1000000))
# 100 loops, best of 3: 3.22 s per loop
And the benchmarks indicate that the direct approach is ~2x to ~3x faster.
(EDITED to write it all as generators and included more edge cases)