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

在 python 列表中獲取唯一的元組,無論順序如何

Grab unique tuples in python list, irrespective of order(在 python 列表中獲取唯一的元組,無論順序如何)
本文介紹了在 python 列表中獲取唯一的元組,無論順序如何的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 python 列表:

[(2,2),(2,3),(1,4),(2,2) 等...]

我需要的是某種功能,將其簡化為獨特的組件...在上面的列表中:

[ (2,2),(2,3),(1,4) ]

<小時>

numpy unique 并沒有完全做到這一點.我可以想到一種方法——將我的元組轉換為數字,[22,23,14,etc.],找到唯一的,然后從那里開始工作......但我沒有不知道復雜性是否會失控.有沒有一個函數可以做我想要對元組做的事情?

<小時>

下面是演示問題的代碼示例:

 將 numpy 導入為 npx = [(2,2),(2,2),(2,3)]y = np.unique(x)

<塊引用>

返回:y: [2 3]

這里是演示修復的解決方案的實現:

 x = [(2,2),(2,2),(2,3)]y = 列表(集合(x))

<塊引用>

返回 y: [(2,2),(2,3)]

解決方案

你可以這樣做

y = np.unique(x, axis=0)z = []對于 y 中的 i:z.append(元組(i))

原因是 numpy 將元組列表解釋為二維數組.通過設置axis = 0,您會要求numpy不要展平數組并返回唯一的行.

I have a python list:

[ (2,2),(2,3),(1,4),(2,2), etc...]

What I need is some kind of function that reduces it to its unique components... which would be, in the above list:

[ (2,2),(2,3),(1,4) ]


numpy unique does not quite do this. I can think of a way to do it--convert my tuples to numbers, [22,23,14,etc.], find the uniques, and work back from there...but I don't know if the complexity won't get out of hand. Is there a function that will do what I am trying to do with tuples?


Here is a sample of code that demonstrates the problem:

 import numpy as np

 x = [(2,2),(2,2),(2,3)]
 y = np.unique(x)

returns: y: [2 3]

And here is the implementation of the solution that demonstrates the fix:

 x = [(2,2),(2,2),(2,3)]
 y = list(set(x))

returns y: [(2,2),(2,3)]

解決方案

you could simply do

y = np.unique(x, axis=0)
z = [] 
for i in y:
   z.append(tuple(i))

The reason is that a list of tuples is interpreted by numpy as a 2D array. By setting axis=0, you'd be asking numpy not to flatten the array and return unique rows.

這篇關于在 python 列表中獲取唯一的元組,無論順序如何的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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.97zyz.com| 国产精品激情 | 国产97人人超碰caoprom | 在线a视频 | 精品欧美一区二区三区精品久久 | 91激情视频 | 波多野结衣av中文字幕 | 欧美激情久久久久久 | 成人午夜免费福利视频 | 久草资源| 欧美性视频在线播放 | 成人在线观看中文字幕 | av影音资源 | 在线看h| 日韩国产中文字幕 | 国产性色视频 | 综合色婷婷 | 特黄小视频 | 涩涩片影院 | 日韩成人在线一区 | 国产精品久久久久久久久免费桃花 | 午夜精品久久久 | 一区中文字幕 | 2019精品手机国产品在线 | 欧美激情一区二区三区 | 国产精品久久久久一区二区三区 |