小编Lan*_*anc的帖子

什么是货币符号的正则表达式?

在java中,我可以使用正则表达式:\p{Sc}用于检测文本中的货币符号.Python中的等价物是什么?

python regex

28
推荐指数
2
解决办法
4327
查看次数

Numpy:找到一个数组中出现在另一个数组中的元素的索引

我有两个1D数组,我想知道一个数组中的元素是否出现在另一个数组中.

例如:

import numpy as np
A = np.array([ 1, 48, 50, 78, 85, 97])
B = np.array([38, 43, 50, 62, 78, 85])
Run Code Online (Sandbox Code Playgroud)

我想要:

C = [2,3,4] # since 50 in second array occurs in first array at index 2, 
            # similarly 78 in second array occurs in first array in index 3,
            # similarly for 85, it is index 4
Run Code Online (Sandbox Code Playgroud)

我试过了:

accuracy = np.searchsorted(A, B)
Run Code Online (Sandbox Code Playgroud)

但它给我带来了不良后果.

python arrays numpy

7
推荐指数
2
解决办法
4006
查看次数

Numpy:按行唯一元素

有谁知道如何在矩阵中逐行获取唯一元素。例如输入矩阵可能类似于:

a = [[1,2,1,3,4,1,3],
     [5,5,3,1,5,1,2],
     [1,2,3,4,5,6,7],
     [9,3,8,2,9,8,4],
     [4,6,7,4,2,3,5]]
Run Code Online (Sandbox Code Playgroud)

它应该返回以下内容:

b = rowWiseUnique(a)
=>  b = [[1,2,3,4,0,0,0],
       [5,3,1,2,0,0,0],
       [1,2,3,4,5,6,7],
       [9,3,8,2,4,0,0],
       [4,6,7,2,3,5,0]]
Run Code Online (Sandbox Code Playgroud)

在 numpy 中执行此操作最有效的方法是什么?我尝试了以下代码,是否有更好、更短的方法来执行此操作?

import numpy as np
def uniqueRowElements(row):
    length = row.shape[0]
    newRow = np.unique(row)
    zerosNumb = length-newRow.shape[0]
    zeros = np.zeros(zerosNumb)
    nR = np.concatenate((newRow,zeros),axis=0)
    return nR    

b = map(uniqueRowElements,a)
b = np.asarray(b)
print b
Run Code Online (Sandbox Code Playgroud)

python numpy scipy

4
推荐指数
1
解决办法
5147
查看次数

标签 统计

python ×3

numpy ×2

arrays ×1

regex ×1

scipy ×1