#### split an array into a list of arrays

``````> (A = matrix(c("a", "b", "a", "c", "b", "d"), nr=3, byrow=TRUE)) # input
[,1] [,2]
[1,] "a"  "b"
[2,] "a"  "c"
[3,] "b"  "d"
> (split.data.frame(A, A[,1])) # output
\$a
[,1] [,2]
[1,] "a"  "b"
[2,] "a"  "c"

\$b
[,1] [,2]
[1,] "b"  "d"
``````

``````import numpy as np
import itertools
A = np.array([["a", "b"], ["a", "c"], ["b", "d"]])
b = a[:,0]

def split(x, f):
return list(itertools.compress(x, f)), list(itertools.compress(x, (not i for i in f)))
split(A, b)

([array(['a', 'b'], dtype='<U1'),
array(['a', 'c'], dtype='<U1'),
array(['b', 'd'], dtype='<U1')],
[])
``````

``````c = np.transpose(np.array([1,1,2]))
np.split(A, c) # returns 4 arrays
``````

2 个回复

### 您可以使用熊猫：

``````import pandas as pd
import numpy as np

a = np.array([["a", "b"], ["a", "c"], ["b", "d"]])

listofdfs = {}
for n,g in pd.DataFrame(a).groupby(0):
listofdfs[n] = g

listofdfs['a'].values
``````

``````array([['a', 'b'],
['a', 'c']], dtype=object)
``````

``````listofdfs['b'].values
``````

``````array([['b', 'd']], dtype=object)
``````

### 或者，您可以使用itertools groupby：

``````import numpy as np
from itertools import groupby
l = [np.stack(list(g)) for k, g in groupby(a, lambda x: x[0])]

l[0]
``````

``````array([['a', 'b'],
['a', 'c']], dtype='<U1')
``````

``````l[1]
``````

``````array([['b', 'd']], dtype='<U1')
``````

``````a = np.array([["a", "b"], ["a", "c"], ["b", "d"]])

x,y=a[:2,:],a[2,:]

x
array([['a', 'b'],
['a', 'c']], dtype='<U1')

y
array(['b', 'd'], dtype='<U1')
``````