给定两个类似以下形式的已排序数组:
a = array([1,2,4,5,6,8,9])
b = array([3,4,7,10])
我希望输出结果为:
c = array([1,2,3,4,5,6,7,8,9,10])
或:
c = array([1,2,3,4,4,5,6,7,8,9,10])
我知道我可以执行以下操作:
c = unique(concatenate((a,b))
我在想是否有更快的方法来处理这个问题,因为我所处理的数组有数百万个元素。
欢迎任何想法。谢谢
由于您使用了numpy,我怀疑bisec并没有帮助到您...所以我建议使用两个小建议:
np.sort
,而是使用c.sort()
方法,该方法原地对数组进行排序并避免复制。np.unique
必须使用不在原地的np.sort
。因此,不要使用np.unique
,可以手动处理逻辑。即先进行排序(原地排序),然后手动执行np.unique
方法(还需检查其Python代码),其中使用flag = np.concatenate(([True], ar[1:] != ar[:-1]))
这个操作可以得到unique = ar[flag]
(其中ar已经被排序)。为了更好,您可能应该直接在原地创建flag数组,即flag = np.ones(len(ar), dtype=bool)
,然后np.not_equal(ar[1:], ar[:-1], out=flag[1:])
,这样可以避免一个完整的flag
副本。.sort
有3种不同的算法,因为您的数组可能已经几乎排好序了,更改排序方法可能会产生速度差异。这将使全部内容接近您获得的结果(在预处理之前不进行去重):
def insort(a, b, kind='mergesort'):
# took mergesort as it seemed a tiny bit faster for my sorted large array try.
c = np.concatenate((a, b)) # we still need to do this unfortunatly.
c.sort(kind=kind)
flag = np.ones(len(c), dtype=bool)
np.not_equal(c[1:], c[:-1], out=flag[1:])
return c[flag]
np.union1d
... - seberg在数组中间插入元素是一种非常低效的操作,因为它们在内存中是平面结构,所以每当你插入另一个元素时,你都需要将其余的所有元素向右移。因此,你可能不想使用bisect。这样做的复杂度大约为O(N ^ 2)。
你当前的方法是O(n * log(n)),所以这已经改善了很多,但还不够完美。
将所有元素插入哈希表(比如set
)也是一种方法。这将花费O(N)的时间来进行去重,但之后你还需要排序,这将花费O(n * log(n))的时间。仍然不是很好。
真正的O(N)解决方案涉及分配一个数组,然后通过取输入列表的最小头部逐个填充它的元素,即合并。不幸的是,似乎没有numpy或Python有这样的东西。解决方案可能是在Cython中编写一个。
它大概会像下面这样:
def foo(numpy.ndarray[int, ndim=1] out,
numpy.ndarray[int, ndim=1] in1,
numpy.ndarray[int, ndim=1] in2):
cdef int i = 0
cdef int j = 0
cdef int k = 0
while (i!=len(in1)) or (j!=len(in2)):
# set out[k] to smaller of in[i] or in[j]
# increment k
# increment one of i or j
O(n)
的时间,因此不会增加复杂度。 - jleahy当你对时间问题感到好奇时,最好的方法就是使用timeit
。下面列出了各种方法及其时间的子集:
import numpy as np
import timeit
import heapq
def insort(a, x, lo=0, hi=None):
if hi is None: hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x < a[mid]: hi = mid
else: lo = mid+1
return lo, np.insert(a, lo, [x])
size=10000
a = np.array(range(size))
b = np.array(range(size))
def op(a,b):
return np.unique(np.concatenate((a,b)))
def martijn(a,b):
c = np.copy(a)
lo = 0
for i in b:
lo, c = insort(c, i, lo)
return c
def martijn2(a,b):
c = np.zeros(len(a) + len(b), a.dtype)
for i, v in enumerate(heapq.merge(a, b)):
c[i] = v
def larsmans(a,b):
return np.array(sorted(set(a) | set(b)))
def larsmans_mod(a,b):
return np.array(set.union(set(a),b))
def sebastian(a, b, kind='mergesort'):
# took mergesort as it seemed a tiny bit faster for my sorted large array try.
c = np.concatenate((a, b)) # we still need to do this unfortunatly.
c.sort(kind=kind)
flag = np.ones(len(c), dtype=bool)
np.not_equal(c[1:], c[:-1], out=flag[1:])
return c[flag]
结果:
martijn2 25.1079499722
OP 1.44831800461
larsmans 9.91507601738
larsmans_mod 5.87612199783
sebastian 3.50475311279e-05
我在这里的具体贡献是 larsmans_mod
,它避免了创建2个集合 - 它只创建一个集合,在这样做的过程中几乎将执行时间缩短了一半。
编辑 删除了martijn
,因为它太慢而无法竞争。 还测试了稍微大一些(排序)输入数组。 我也没有测试输出的正确性...
c
数组正在折磨我。 - Martijn Pietersbisect.insort
的其他答案外,如果您对性能不满意,可以尝试使用 blist
模块与 bisect
。它应该会提高性能。list
插入复杂度为 O(n)
,而 blist
的插入复杂度为 O(log(n))
。heapq
模块的 merge
函数利用两个数组都已排序的事实。这种方法将需要额外的开销,因为会在内存中创建一个新的数组。这可能是要考虑的选择,因为该解决方案的时间复杂度为 O(n+m)
,而使用 insort 的解决方案的复杂度为 O(n*m)
(n个元素 * m次插入)。import heapq
a = [1,2,4,5,6,8,9]
b = [3,4,7,10]
it = heapq.merge(a,b) #iterator consisting of merged elements of a and b
L = list(it) #list made of it
print(L)
输出:
[1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10]
如果您想删除重复值,可以使用 groupby:
import heapq
import itertools
a = [1,2,4,5,6,8,9]
b = [3,4,7,10]
it = heapq.merge(a,b) #iterator consisting of merged elements of a and b
it = (k for k,v in itertools.groupby(it))
L = list(it) #list made of it
print(L)
输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
O(n)
的时间复杂度。而在blist
中,他们使用另一种数据结构,因此复杂度为O(log(n))
。对于大型数组和列表,这将极大地改变事情的发展。 - ovgolovinbisect
模块来进行此类合并,将第二个Python列表合并到第一个列表中。
bisect*
函数适用于numpy数组,但insort*
函数不适用。很容易使用模块源代码来调整算法,它非常基础:from numpy import array, copy, insert
def insort(a, x, lo=0, hi=None):
if hi is None: hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x < a[mid]: hi = mid
else: lo = mid+1
return lo, insert(a, lo, [x])
a = array([1,2,4,5,6,8,9])
b = array([3,4,7,10])
c = copy(a)
lo = 0
for i in b:
lo, c = insort(c, i, lo)
虽然自定义的insort
在这里并没有添加任何内容,但默认的bisect.bisect
也能很好地工作:
import bisect
c = copy(a)
lo = 0
for i in b:
lo = bisect.bisect(c, i)
c = insert(c, i, lo)
insort
比组合和排序更加高效。因为b
也是排序的,所以我们可以跟踪lo
插入点并从那里开始搜索下一个点,而不是在每次循环中考虑整个数组。a
,只需直接对该数组进行操作并保存副本。heapq.merge
:from numpy import zeros
import heapq
c = zeros(len(a) + len(b), a.dtype)
for i, v in enumerate(heapq.merge(a, b)):
c[i] = v
insert
方法。 - mgilson似乎没有人提到union1d
(union1d)。目前,它是unique(concatenate((ar1, ar2)))
的快捷方式,但它是一个易于记忆的短名称,并且由于它是一个库函数,有潜力被numpy开发人员进行优化。对于大型数组,它的性能非常类似于seberg所接受的答案中的insort
。这是我的基准测试:
import numpy as np
def insort(a, b, kind='mergesort'):
# took mergesort as it seemed a tiny bit faster for my sorted large array try.
c = np.concatenate((a, b)) # we still need to do this unfortunatly.
c.sort(kind=kind)
flag = np.ones(len(c), dtype=bool)
np.not_equal(c[1:], c[:-1], out=flag[1:])
return c[flag]
size = int(1e7)
a = np.random.randint(np.iinfo(np.int).min, np.iinfo(np.int).max, size)
b = np.random.randint(np.iinfo(np.int).min, np.iinfo(np.int).max, size)
np.testing.assert_array_equal(insort(a, b), np.union1d(a, b))
import timeit
repetitions = 20
print("insort: %.5fs" % (timeit.timeit("insort(a, b)", "from __main__ import a, b, insort", number=repetitions)/repetitions,))
print("union1d: %.5fs" % (timeit.timeit("np.union1d(a, b)", "from __main__ import a, b; import numpy as np", number=repetitions)/repetitions,))
我的机器上的输出:
insort: 1.69962s
union1d: 1.66338s
sort
,因为从unique
输出的结果已经保证是排序过的。 - Fred Foo