久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚洲v视频 | 日本在线观看一区 | 天堂av影院 | 亚洲精品在 | 国产精品成人免费视频 | 成人午夜视频在线观看 | 91精选视频 | 国产一区二区三区免费视频 | 欧美999| 玖玖精品在线 | 国产日韩欧美 | 成年人免费看视频 | 欧美大白屁股 | av一区二区三区 | 欧美亚洲在线观看 | 国产日韩精品一区二区 | 91蜜桃婷婷狠狠久久综合9色 | 成人在线小视频 | 国产乱码一区二区 | 狠狠久 | 国产a久久麻豆入口 | 久久久久久精 | 国产在线观看一区 | 国产极品国产极品 | 欧美成人免费在线视频 | 欧美福利在线 | 欧美人与野 | 国产伦精品一区二区三区四区 | 日韩色综合 | 91成年人 | 日日爽天天 | 香蕉视频导航 | 国产日韩精品在线 | 最新免费黄色网址 | 婷婷视频网 | 久久免费国产视频 | 久久99热这里只频精品6学生 | 手机看片日韩 | 久久av资源 | 国产高清视频在线 | 中文在线一区 |