在Python / numpy中,如何根据它的第一列(即索引)来拆解/拆分一个数组?

时间:2021-10-25 21:23:13

This question is closely related to How to split an array according to a condition in numpy? but I am looking for a more general way to split an array given an unknown number of indices:

这个问题与如何根据numpy中的条件分割数组密切相关。但我正在寻找一种更通用的方法来分割一个给定数量未知的索引的数组:

import numpy as np
a=np.arange(10,40).reshape(10,3)
b=np.array([[1],[1],[1],[2],[2],[3],[3],[4],[4],[5]])
c=np.hstack((b,a))

array([[ 1, 10, 11, 12],
       [ 1, 13, 14, 15],
       [ 1, 16, 17, 18],
       [ 2, 19, 20, 21],
       [ 2, 22, 23, 24],
       [ 3, 25, 26, 27],
       [ 3, 28, 29, 30],
       [ 4, 31, 32, 33],
       [ 4, 34, 35, 36],
       [ 5, 37, 38, 39]])

I'd like to split this up into a 1x2x2 array according to the first column, thus:

我想根据第一列将它分割成一个1x2x2数组,因此:

array([[[ 1, 10, 11, 12],
        [ 1, 13, 14, 15],
        [ 1, 16, 17, 18]],

       [[ 2, 19, 20, 21],
        [ 2, 22, 23, 24]],

       [[ 3, 25, 26, 27],
        [ 3, 28, 29, 30]],

       [[ 4, 31, 32, 33],
        [ 4, 34, 35, 36]],

       [[ 5, 37, 38, 39]]])

I am new to Python, so thank you in advance for your help!

我是Python的新手,所以非常感谢您的帮助!

1 个解决方案

#1


4  

In pure python you can do it using itertools.groupby:

在纯python中,您可以使用itertools.groupby:

>>> from operator import itemgetter
>>> from itertools import groupby
>>> from pprint import pprint
>>> pprint ([list(g) for k, g in groupby(c, key=itemgetter(0))])
[[array([ 1, 10, 11, 12]), array([ 1, 13, 14, 15]), array([ 1, 16, 17, 18])],
 [array([ 2, 19, 20, 21]), array([ 2, 22, 23, 24])],
 [array([ 3, 25, 26, 27]), array([ 3, 28, 29, 30])],
 [array([ 4, 31, 32, 33]), array([ 4, 34, 35, 36])],
 [array([ 5, 37, 38, 39])]]

Using NumPy:

使用NumPy:

>>> e, inds = np.unique(c[:,0], return_index=True)
>>> np.split(c, inds)[1:]
[array([[ 1, 10, 11, 12],
       [ 1, 13, 14, 15],
       [ 1, 16, 17, 18]]),
 array([[ 2, 19, 20, 21],
       [ 2, 22, 23, 24]]),
 array([[ 3, 25, 26, 27],
       [ 3, 28, 29, 30]]),
 array([[ 4, 31, 32, 33],
       [ 4, 34, 35, 36]]),
 array([[ 5, 37, 38, 39]])]

#1


4  

In pure python you can do it using itertools.groupby:

在纯python中,您可以使用itertools.groupby:

>>> from operator import itemgetter
>>> from itertools import groupby
>>> from pprint import pprint
>>> pprint ([list(g) for k, g in groupby(c, key=itemgetter(0))])
[[array([ 1, 10, 11, 12]), array([ 1, 13, 14, 15]), array([ 1, 16, 17, 18])],
 [array([ 2, 19, 20, 21]), array([ 2, 22, 23, 24])],
 [array([ 3, 25, 26, 27]), array([ 3, 28, 29, 30])],
 [array([ 4, 31, 32, 33]), array([ 4, 34, 35, 36])],
 [array([ 5, 37, 38, 39])]]

Using NumPy:

使用NumPy:

>>> e, inds = np.unique(c[:,0], return_index=True)
>>> np.split(c, inds)[1:]
[array([[ 1, 10, 11, 12],
       [ 1, 13, 14, 15],
       [ 1, 16, 17, 18]]),
 array([[ 2, 19, 20, 21],
       [ 2, 22, 23, 24]]),
 array([[ 3, 25, 26, 27],
       [ 3, 28, 29, 30]]),
 array([[ 4, 31, 32, 33],
       [ 4, 34, 35, 36]]),
 array([[ 5, 37, 38, 39]])]