Python:如何从2个数组中找到唯一的元素模式?

标签 python arrays numpy

我有两个 numpy 数组,AB:

A = ([1, 2, 3, 2, 3, 1, 2, 1, 3])
B = ([2, 3, 1, 2])

其中 BA 中的唯一模式。

我需要输出为 A 中的所有元素,而 B 中不存在这些元素。

Output = ([1, 2, 3, 1, 3])

最佳答案

最简单的方法是使用 Python 的内置函数,即字符串类型:

A = "123231213"
B = "2312"
result = A.replace(B, "")

要有效地将 numpy.array 转换为 str,请使用以下函数:

x = numpy.frombuffer("3452353", dtype="|i1")
x
array([51, 52, 53, 50, 51, 53, 51], dtype=int8)
x.tostring()
"3452353"

(*) 因此混合了 ascii 代码 (1 != "1"),但子字符串搜索可以正常工作。您的数据类型应该更适合一个字符,否则您可能会得到错误的匹配。

总而言之,快速破解如下:

A = numpy.array([1, 2, 3, 2, 3, 1, 2, 1, 3])
B = numpy.array([2, 3, 1, 2])
numpy.fromstring(A.tostring().replace(B.tostring(), ""), dtype=A.dtype)
array([1, 2, 3, 1, 3])
# note, here dtype is some int, I'm relying on the fact that:
# "1 matches 1" is equivalent to "0001 matches 00001"
# this holds as long as values of B are typically non-zero.
#
# this trick can conceptually be used with floating point too,
# but beware of multiple floating point representations of same number

深入解释:

假设A和B的大小是任意的,naive approach以二次方时间运行。然而更好的是,存在概率算法,例如 Rabin-Karp ,它依赖于滑动窗口哈希。

这是面向文本的函数(例如 xxx in strstr.replacere )比自定义函数快得多的主要原因numpy 代码。

如果你确实需要这个函数与 numpy 集成,你总是可以编写一个扩展,但这并不容易:)

关于Python:如何从2个数组中找到唯一的元素模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23056215/

相关文章:

javascript - 一键点击后在javascript中的两个div中设置两个不同的图像

Python 进程在 Numpy 数组中仅使用 1.6 GB RAM Ubuntu 32 位

python-2.7 - ndarray的Numpy堆栈列表

python - 按具有多个值的列对 DF 进行排序

python - JustHost.com 和 Python CGI

java - 数组和Java字符串错误 : [Ljava. lang.String;@19c42c4b

python - Cython:cimport 和 import numpy as (both) np

Python 2.7 没有名为 _sqlite3 的模块(没有根访问机器,本地安装了 python)?

python - discord.py - 发送文件而不在我的计算机上创建文件?

javascript - 如果除 2 之外都是假的,如何检查数组索引 0 上的每个项目?