首页 > 解决方案 > 字符串的快速连接

问题描述

我有一个二维 0/1 数组,X. 每列代表一个特定的字母。对于每一行,我想加入那些X值为 1 的字母。

例如:

import numpy as np
abc = np.array(['A','B','C','D','E','F'],dtype=str)
X = np.random.randint(0,2,(5,abc.shape[0]))

res = [np.string_.join('',abc[row==1]) for row in X]

这很好,只是这个特定任务是我的代码的瓶颈。因此,我尝试将其移至 cython 没有成功,这主要是由于我对字符串和字符等的理解非常有限。下面的代码供参考,但它很糟糕。这一次,它并没有完全返回我想要的(例如,必须将字符转换为 Python 字符串),更令人担忧的是,我认为代码不稳定。

import numpy as np
cimport numpy as np
cimport cython 

from libc.stdlib cimport malloc, free 

def join_c(int[:,:] idx, bytes abc):

    cdef:
        size_t i, j, count
        int n = idx.shape[0]
        int m = idx.shape[1]

        char *arr = <char *>malloc((n*(m+1))*sizeof(char))

    count = 0
    try:        
        for i in range(n):
            for j in range(m):
                if idx[i,j] == 1:

                    arr[count] = abc[j]
                    count +=1 

            arr[count] = ','
            count+=1

        return [x for x in arr]

    finally:
        free(arr)   

我想看看如何在 cython 中做到这一点,但我对任何其他快速解决方案都很满意。

标签: pythonstringnumpycython

解决方案


这是一个基于字符串数组的解决方案 -

def join_singlechars(abc, X):
    # Get mask
    mask = X==1

    # Get start, stop indices for splitting the concatenated string later on
    idx = np.r_[0,mask.sum(1).cumsum()]

    # Get concatenated string
    n = idx[-1] #sum of 1s in mask          
    s = np.broadcast_to(abc, X.shape)[mask].tostring()
    # Or np.broadcast_to(abc, X.shape)[mask].view('S'+str(n))[0]

    return [s[i:j] for i,j in zip(idx[:-1],idx[1:])] # finally split

样品运行 -

In [229]: abc
Out[229]: array(['A', 'B', 'C', 'D', 'E', 'F'], dtype='|S1')

In [230]: X
Out[230]: 
array([[1, 0, 1, 0, 0, 1],
       [1, 1, 0, 1, 1, 0],
       [1, 0, 1, 1, 0, 0],
       [1, 1, 0, 1, 1, 1],
       [1, 1, 1, 0, 0, 1]])

In [231]: join_singlechars(abc, X)
Out[231]: ['ACF', 'ABDE', 'ACD', 'ABDEF', 'ABCF']

大型5000 x 5000阵列案例的计时 -

In [321]: abc = np.array(['A','B','C','D','E','F'],dtype=str)
     ...: abc = np.resize(abc,5000)
     ...: np.random.seed(0)
     ...: X = np.random.randint(0,2,(5000,5000))

In [322]: %timeit [np.string_.join('',abc[row==1]) for row in X]
1 loop, best of 3: 648 ms per loop

In [323]: %timeit join_singlechars(abc, X)
1 loop, best of 3: 209 ms per loop

推荐阅读