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

TypeError: unhashable type: 'list' when using built-i

TypeError: unhashable type: #39;list#39; when using built-in set function(TypeError: unhashable type: list when using built-in set function)
本文介紹了TypeError: unhashable type: 'list' when using built-in set function的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個包含多個列表作為其元素的列表

I have a list containing multiple lists as its elements

eg: [[1,2,3,4],[4,5,6,7]]

如果我使用內置的 set 函數從這個列表中刪除重復項,我會收到錯誤

If I use the built in set function to remove duplicates from this list, I get the error

TypeError: unhashable type: 'list'

我使用的代碼是

TopP = sorted(set(TopP),reverse=True)

TopP 是一個列表,就像在例如以上

Where TopP is a list just like in the e.g. Above

set() 的這種用法是錯誤的嗎?還有其他方法可以對上述列表進行排序嗎?

Is this usage of set() wrong? Is there any other way in which I can sort the above list?

推薦答案

Set 要求它們的項目是 hashable.在 Python 預定義的類型中,只有不可變的類型(例如字符串、數字和元組)是可散列的.可變類型(例如列表和字典)不可散列,因為更改其內容會更改散列并破壞查找代碼.

Sets require their items to be hashable. Out of types predefined by Python only the immutable ones, such as strings, numbers, and tuples, are hashable. Mutable types, such as lists and dicts, are not hashable because a change of their contents would change the hash and break the lookup code.

因為您無論如何都要對列表進行排序,所以只需將重復刪除放置在列表已排序的之后.這很容易實現,不會增加操作的算法復雜度,并且不需要將子列表更改為元組:

Since you're sorting the list anyway, just place the duplicate removal after the list is already sorted. This is easy to implement, doesn't increase algorithmic complexity of the operation, and doesn't require changing sublists to tuples:

def uniq(lst):
    last = object()
    for item in lst:
        if item == last:
            continue
        yield item
        last = item

def sort_and_deduplicate(l):
    return list(uniq(sorted(l, reverse=True)))

這篇關于TypeError: unhashable type: 'list' when using built-in set function的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产.com| 欧美成人猛片aaaaaaa | 理论片午午伦夜理片影院 | 精品一区国产 | 中文字幕国产视频 | 人人99 | 欧美视频一区 | 亚洲精品91 | 狠狠色狠狠色综合系列 | 午夜无码国产理论在线 | 亚洲成人综合在线 | 欧美日韩久久精品 | 一区二区三区精品视频 | 国产乱码精品一区二区三区五月婷 | 日韩一二区 | 国产视频欧美 | 成人福利网站 | 欧美久久久久 | 国产高清视频在线观看 | www.久久.com| 一级在线观看 | 欧美精品久久久久 | 亚洲视频精品 | 国产欧美一区二区三区久久人妖 | 黄色一级网 | 91视频免费视频 | 久久久久久久久精 | 亚洲欧美一区二区三区情侣bbw | 91人人看 | 99re热精品视频国产免费 | 少妇一级淫片免费播放 | 欧美精品在线播放 | 欧美激情精品久久久久久 | 91看片免费版 | 欧美一区二区三区在线 | 国产清纯白嫩初高生在线播放视频 | 亚洲精品一区二区网址 | 精品视频一区二区三区 | 亚洲精品中文在线 | 毛片毛片毛片毛片毛片 | 亚洲精彩视频 |