久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 日韩电影免费观看中文字幕 | 黄色片亚洲 | 国产精品久久久久免费 | 日本成人免费网站 | 日本一区二区高清不卡 | 久久国产精品72免费观看 | 91九色麻豆 | 免费观看一级视频 | 国产成人精品免费视频大全最热 | 99reav | 色av一区二区三区 | 日韩欧美在线播放 | 一区二区三区在线免费看 | 亚州精品天堂中文字幕 | 国产欧美日韩精品一区 | 99国产精品久久久久老师 | av网站在线看 | 国产成人精品一区二区三区四区 | 成人国产精品久久 | 精品国产乱码久久久久久蜜退臀 | 天天操网| 一区二区三区视频在线 | 黄色在线免费观看视频 | 免费视频久久 | 91久久北条麻妃一区二区三区 | 色综合网站 | 亚洲精品久久久久国产 | 国产日韩av一区二区 | 91高清在线观看 | 国产精品久久国产精品99 | 99精品国产一区二区青青牛奶 | 午夜影院黄 | 最新日韩在线 | 国产精品高潮呻吟久久av野狼 | 日韩成人中文字幕 | 日韩欧美一区二区三区 | 三级免费 | av日韩精品 | 福利视频一二区 | 中文字幕一区二区三区精彩视频 | 国产在线精品一区二区三区 |