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

從列表中刪除重復項和原始項

Remove duplicates and original from list(從列表中刪除重復項和原始項)
本文介紹了從列表中刪除重復項和原始項的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

給定一個字符串列表,我想刪除重復的單詞和原始單詞.

Given a list of strings I want to remove the duplicates and original word.

例如:

lst = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']

輸出應刪除重復項,所以像這樣 ['a', 'b', 'd']

The output should have the duplicates removed, so something like this ['a', 'b', 'd']

我不需要保留訂單.

推薦答案

使用 collections.Counter() object,然后只保留那些計數為 1 的值:

Use a collections.Counter() object, then keep only those values with a count of 1:

from collections import counter

[k for k, v in Counter(lst).items() if v == 1]

這是一個 O(N) 算法;您只需要遍歷 N 個項目的列表一次,然后再對更少的項目 (< N) 進行第二次循環,以提取那些只出現一次的值.

This is a O(N) algorithm; you just need to loop through the list of N items once, then a second loop over fewer items (< N) to extract those values that appear just once.

如果順序很重要并且您正在使用 Python <3.6、分離步驟:

If order is important and you are using Python < 3.6, separate the steps:

counts = Counter(lst)
[k for k in lst if counts[k] == 1]

演示:

>>> from collections import Counter
>>> lst = ['a', 'b', 'c', 'c', 'c', 'd', 'e', 'e']
>>> [k for k, v in Counter(lst).items() if v == 1]
['a', 'b', 'd']
>>> counts = Counter(lst)
>>> [k for k in lst if counts[k] == 1]
['a', 'b', 'd']

兩種方法的順序相同是巧合;對于 Python 3.6 之前的 Python 版本,其他輸入可能會導致不同的順序.

That the order is the same for both approaches is a coincidence; for Python versions before Python 3.6, other inputs may result in a different order.

在 Python 3.6 中,字典的實現發生了變化,現在保留了輸入順序.

In Python 3.6 the implementation for dictionaries changed and input order is now retained.

這篇關于從列表中刪除重復項和原始項的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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.日韩精品 | 一本到av | 亚洲激情中文字幕 | av资源在线 | 永久免费看mv网站入口亚洲 | 日本一级淫片色费放 | 日韩免费视频 | 久久草av | 欧美日韩国产一区二区 | 哦┅┅快┅┅用力啊┅aps | 日韩在线视频一区 | 天天干天天干天天 | 国产精品毛片av | 特黄av| 日韩国产精品一区二区 | 国产成人午夜高潮毛片 | 国产黄av| 精品欧美一区二区三区久久久 | 91福利视频导航 | 综合激情网 | 国产一区二区三区在线视频 | 蜜桃久久久 | 色综合视频在线观看 | 日韩一级免费视频 | 精品视频一区二区三区四区 | 日本在线免费视频 | 欧美日韩高清在线 | 成人精品在线视频 | 免费的av | 亚洲激情综合网 | 亚洲一区二区免费看 | 日韩中文在线观看 | 国产成人精品亚洲男人的天堂 | 亚洲福利网站 | 一区二区三区日韩 | 日本三极片 | 免费视频毛片 | 人人爱人人草 | av大片在线观看 | 在线免费看黄网站 |