問題描述
我想刪除彼此跟隨的重復項,但不刪除整個數組中的重復項.另外,我想保持順序不變.
I would like to remove duplicates which follow each other, but not duplicates along the whole array. Also, I want to keep the ordering unchanged.
所以如果輸入是 [0 0 1 3 2 2 3 3]
輸出應該是 [0 1 3 2 3]
So if the input is [0 0 1 3 2 2 3 3]
the output should be [0 1 3 2 3]
我找到了一種使用 itertools.groupby()
的方法,但我正在尋找更快的 NumPy 解決方案.
I found a way using itertools.groupby()
but I am looking for a faster NumPy solution.
推薦答案
a[np.insert(np.diff(a).astype(np.bool), 0, True)]
Out[99]: array([0, 1, 3, 2, 3])
一般的思路是使用diff
來查找數組中兩個連續元素之間的差異.然后我們只索引那些給出 non-zero
差異元素的元素.但是由于 diff
的長度短了 1.所以在索引之前,我們需要 insert
將 True
到 diff 數組的開頭.
The general idea is to use diff
to find the difference between two consecutive elements in the array. Then we only index those which give non-zero
differences elements. But since the length of diff
is shorter by 1. So before indexing, we need to insert
the True
to the beginning of the diff array.
說明:
In [100]: a
Out[100]: array([0, 0, 1, 3, 2, 2, 3, 3])
In [101]: diff = np.diff(a).astype(np.bool)
In [102]: diff
Out[102]: array([False, True, True, True, False, True, False], dtype=bool)
In [103]: idx = np.insert(diff, 0, True)
In [104]: idx
Out[104]: array([ True, False, True, True, True, False, True, False], dtype=bool)
In [105]: a[idx]
Out[105]: array([0, 1, 3, 2, 3])
這篇關于刪除 NumPy 數組中的連續重復項的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!