久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

刪除 NumPy 數組中的連續重復項

Remove consecutive duplicates in a NumPy array(刪除 NumPy 數組中的連續重復項)
本文介紹了刪除 NumPy 數組中的連續重復項的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想刪除彼此跟隨的重復項,但不刪除整個數組中的重復項.另外,我想保持順序不變.

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.所以在索引之前,我們需要 insertTrue 到 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧洲视频一区二区 | 午夜一区 | 久久激情五月丁香伊人 | 韩国电影久久 | 四虎在线视频 | 久久久www成人免费精品 | 蜜桃精品在线 | 最近最新中文字幕 | 精品一区二区三区在线观看国产 | 欧美日韩电影一区 | 国产91在线播放 | 久久久一区二区三区 | 免费成人毛片 | 国产午夜精品视频 | 亚洲精品一区二区网址 | 国产免费拔擦拔擦8x高清 | 中文字幕91 | 成人国产在线观看 | 隔壁老王国产在线精品 | 欧美精品在线一区二区三区 | 亚洲视频在线一区 | 欧美国产日韩一区二区三区 | 先锋资源亚洲 | h视频在线播放 | 国产精品一区三区 | 国产精品久久久久久久久久久久久 | 欧美日韩三级视频 | 欧美性久久 | 久久噜| 亚洲中午字幕 | 中文字幕一区二区三区日韩精品 | 超碰97人人人人人蜜桃 | 久久99精品久久久 | 国产成人精品一区二区三区在线 | 久久不射电影网 | 亚洲精品一区在线观看 | 97视频精品 | 成人日韩精品 | 国产ts人妖系列高潮 | 欧美日韩中文字幕在线 | 天天插天天操 |