How to split a list into chunks determined by a separator?

Question:

I have this list (python):

[[item1],[item2],[item3],[/],[item4],[item5],[item6],[/]...]

I want to separate these into chunks and the elements that will go into each chunk are the elements before the separator "/".

So my chunks would look like:

chunk1 = [[item1],[item2],[item3]]
chunk2 = [[item4],[item5],[item6]]

I’ve tried and tried, but nothing efficient came to mind. Tried looping through it with a for and and if element[x] == ‘/’ then get some positions. It’s very dirty and doesn’t properly work.

Any help would be appreciated.

Asked By: Benjamin

||

Answers:

The usual approach for collecting contiguous chunks is to use itertools.groupby, for example:

>>> from itertools import groupby
>>> blist = ['item1', 'item2', 'item3', '/', 'item4', 'item5', 'item6', '/']
>>> chunks = (list(g) for k,g in groupby(blist, key=lambda x: x != '/') if k)
>>> for chunk in chunks:
...     print(chunk)
...     
['item1', 'item2', 'item3']
['item4', 'item5', 'item6']

(Your representation of your list [item1],[item2],[item3],[/], makes it look like each of your elements in the list is actually a list, in which case the same approach will work, you simply need to compare against ['/'] or whatever your separator is.)

Answered By: DSM

I wrote something simpler for you to understand – Basically look out for '/', if it’s not there keep appending to chunks. itertools.groupby would be worth learning, but something simpler that one understands first is a good idea to start with.

l = ['i1', 'i2', 'i3', '/', 'i4', 'i5', 'i6', '/']

chunks = []
x = 0
chunks.append([])   # create an empty chunk to which we'd append in the loop
for i in l:
    if i != '/':
        chunks[x].append(i)
    else:
        x += 1
        chunks.append([])

print chunks

If your elements are strings, there’s a faster way to do what I have done in python – basically – first create a ' ' (space) separated string and then, first split by '/' and then by ' ' again.

l = ['i1', 'i2', 'i3', '/', 'i4', 'i5', 'i6', '/']

s = " ".join(l)  # first create a string, joining by a <space> it could be anything

chunks2 = [x.split() for x in s.split("/")]
print chunks2
Answered By: gabhijit

This can also be done as (assuming empty chunks are not desired and l is the list to be “chunked”):

chunks, last_chunk = [], []
for x in l:
    if x == '/':
         if last_chunk:
             chunks.append(last_chunk)
             last_chunk = []
    else:
         last_chunk.append(x)
if last_chunk:
    chunks.append(last_chunk)
Answered By: dcg

Less flexible than the groupby-solutions, but in case someone is anyway going to use Numpy arrays, and there is only one (or a fixed small number) of delimiters:

i, = np.where(array_of_str=='/')[0]
bulk1, bulk2 = array_of_str[:i], array_of_str[i+1:]
Answered By: sigvaldm
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.