user1618465
user1618465

Reputation: 1941

Python use split with arrays

Is there any equivalent to split for arrays?

a = [1, 3, 4, 6, 8, 5, 3, 4, 5, 8, 4, 3]

separator = [3, 4] (len(separator) can be any)

b = a.split(separator)

b = [[1], [6, 8, 5], [5, 8, 4, 3]]

Upvotes: 6

Views: 9540

Answers (4)

B. M.
B. M.

Reputation: 18628

For efficiency (50 x) on big arrays, there is a np.split method which can be used. the difficulty is to delete the separator :

from pylab import *
a=randint(0,3,10)
separator=arange(2)
ind=arange(len(a)-len(separator)+1) # splitting indexes
for i in range(len(separator)): ind=ind[a[ind]==separator[i]]+1 #select good candidates
cut=dstack((ind-len(separator),ind)).flatten() # begin and end
res=np.split(a,cut)[::2] # delete separators
print(a,cut,res)

gives:

[0 1 2 0 1 1 2 0 1 1] [0 2 3 5 7 9] [[],[2],[1, 2],[1]]

Upvotes: 0

Eugene Yarmash
Eugene Yarmash

Reputation: 149736

You could join the list to a string using a non-numeric separator, then do the split:

>>> s = " {} ".format(" ".join(map(str, a)))  
>>> s
' 1 3 4 6 8 5 3 4 5 8 4 3 '
>>> [[int(y) for y in x.split()] for x in s.split(" 3 4 ")]
[[1], [6, 8, 5], [5, 8, 4, 3]]

The 2 extra spaces in the string take account of edge cases (e.g. a = [1, 3, 4]).

Upvotes: 0

Matthew
Matthew

Reputation: 7590

No, but we could write a function to do such a thing, and then if you need it to be an instance method you could subclass or encapsulate list.

def separate(array,separator):
    results = []
    a = array[:]
    i = 0
    while i<=len(a)-len(separator):
        if a[i:i+len(separator)]==separator:
            results.append(a[:i])
            a = a[i+len(separator):]
            i = 0
        else: i+=1
   results.append(a)
   return results

If you wanted this to work as an instance method, we could do the following to encapsulate the list:

class SplitableList:
    def __init__(self,ar): self.ary = ar
    def split(self,sep): return separate(self.ary,sep)
    # delegate other method calls to self.ary here, for example
    def __len__(self): return len(self.ary)

a = SplitableList([1,3,4,6,8,5,3,4,5,8,4,3])
b = a.split([3,4]) # returns desired result

or we could subclass list like so:

class SplitableList(list):
    def split(self,sep): return separate(self,sep)

a = SplitableList([1,3,4,6,8,5,3,4,5,8,4,3])
b = a.split([3,4]) # returns desired result

Upvotes: 4

Yoav Glazner
Yoav Glazner

Reputation: 8066

No there is not. You will have to write your own

or take this one:

def split(a, sep):
    pos = i = 0
    while i < len(a):
        if a[i:i+len(sep)] == sep:
            yield a[pos:i]
            pos = i = i+len(sep)
        else:
            i += 1
    yield a[pos:i]

print list(split(a, sep=[3, 4]))

Upvotes: 3

Related Questions