Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a list into chunks determined by a separator

Tags:

python

split

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, 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.

like image 887
Benjamin Avatar asked Jun 14 '15 02:06

Benjamin


People also ask

How do you split a list by delimiter in Python?

Use split() method to split by delimiter. If the argument is omitted, it will be split by whitespace, such as spaces, newlines \n , and tabs \t . Consecutive whitespace is processed together. A list of the words is returned.

How do you split data in a list?

The split() method of the string class is fairly straightforward. It splits the string, given a delimiter, and returns a list consisting of the elements split out from the string. By default, the delimiter is set to a whitespace - so if you omit the delimiter argument, your string will be split on each whitespace.

Can you split () a list in Python?

To split a list into n parts in Python, use the numpy. array_split() function. The np. split() function splits the array into multiple sub-arrays.


3 Answers

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
like image 151
gabhijit Avatar answered Sep 21 '22 07:09

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)
like image 37
dcg Avatar answered Sep 21 '22 07:09

dcg


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.)

like image 36
DSM Avatar answered Sep 20 '22 07:09

DSM