久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美成人精品欧美一级 | 自拍偷拍亚洲欧美 | 免费簧片视频 | 黄色成人在线观看 | 天天综合日日夜夜 | 国产美女一区二区 | 欧美在线综合 | 日韩中文字幕av | 91色视频在线观看 | 91视频.| www.激情.com| 欧美a区 | 欧美成人第一页 | 国产视频观看 | 久久久久国产精品一区二区 | 日韩在线视频一区 | 视频在线一区二区 | 久久成人一区 | 成人h视频在线 | av网站在线播放 | 拍真实国产伦偷精品 | 亚洲第一视频网站 | 久久国产精品72免费观看 | 福利av在线| 黄免费观看视频 | 午夜在线| 毛片av免费在线观看 | 欧美一区二区黄 | av免费网站在线观看 | 久视频在线观看 | 中文字幕av中文字幕 | 欧美一卡二卡在线观看 | 亚洲欧美精品 | 国产99久久久国产精品 | 亚洲精品观看 | 国产九九精品 | 日韩综合网 | 日日夜夜天天 | 国产午夜精品一区二区三区在线观看 | 中文字幕电影在线观看 | 日韩欧美在线免费观看 |