Skip to content Skip to sidebar Skip to footer

Python: How To Split A List Into An Unknown Number Of Smaller Lists Based On A Delimeter

I've got a list which contains the following strings: MainList '00:00' '00:01' '00:02' '00:03' '00:04' '00:00' '00:01' '00:02' '00:03' '00:04' I would like to split this into a s

Solution 1:

I usually do this:

def splitby( lst, breaker='00:00'):
    current = []
    it = iter(lst)
    first = next(it)
    assert first==breaker, "`lst` must begin with `breaker`"
    for item in it:
        if item == breaker:
            yield current
            current = []
        current.append(item)
    yield current

The inevitable itertools solution is a bit more general:

from itertools import groupby

class splitter(object):

    def __init__(self, breaker):
        self.breaker = breaker
        self.current_group = 0

    def __call__(self, item):
        if item == self.breaker:
            self.current_group+=1
        return self.current_group

    def group(self, items):
        return (list(v) for k,v in groupby(items,self))

print list(splitter('00:00').group(items))

Solution 2:

In an explicit way, you could do like this :

sep = '00:00'
split_list = []
for item in Mainlist:
    if item == sep:
        split_list.append([item])
    else:
        split_list[-1].append(item)

print split_list

Solution 3:

Comprehensions is your best friend :). Just two lines:

>>> a=['00:00', '00:01', '00:02', '00:03', '00:00', '00:01', '00:02']
>>> found=[index for index,item in enumerate(a) if item=='00:00'] + [len(a)]
>>> [a[found[i]:found[i+1]] for i in range(len(found)-1)]
[['00:00', '00:01', '00:02', '00:03'], ['00:00', '00:01', '00:02']]

Here is what we do:

We search for delimiter positions and get a list which contains delimiter indexes:

>>> found=[index for index,item in enumerate(a) if item=='00:00']
>>> found
[0, 4]

We're adding len(a) for including the last dict.

And creating new lists with splitting a with founded indexes :

>>> [a[found[i]:found[i+1]] for i in range(len(found)-1)]
[['00:00', '00:01', '00:02', '00:03'], ['00:00', '00:01', '00:02']]

Solution 4:

I could think of another way :-)

def list_split(a):
    #a=['00:00', '00:01', '00:02', '00:03', '00:00', '00:01', '00:02']
    output = []
    count = 0

    if len(a) < 1:
        output.append(a)
        return output

    for i, item in enumerate(a[1:]):
        if item == a[0]:
            output.append(a[count:i+1])
            count = i + 1
    else:
        output.append(a[count:])
        return output

Post a Comment for "Python: How To Split A List Into An Unknown Number Of Smaller Lists Based On A Delimeter"