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

兩個列表之間沒有重復的公共元素

Common elements between two lists with no duplicates(兩個列表之間沒有重復的公共元素)
本文介紹了兩個列表之間沒有重復的公共元素的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

問題是這樣的,取兩個列表,比如說這兩個:

Problem is this, take two lists, say for example these two:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

并編寫一個程序,該程序返回一個列表,該列表僅包含列表之間共有的元素(沒有重復).確保您的程序適用于兩個不同大小的列表.

And write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.

這是我的代碼:

a = [1, 1, 2, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
c = []
for i in a:
    if i in b and i not in c:
        c.append([i])
print(c)

盡管有i not in c"語句,但我的輸出仍然給我重復.為什么是這樣?我敢肯定它很明顯,我只是看不到它!

My output is still giving me duplicates despite the 'i not in c' statement. why is this? I'm sure its blatantly obvious, I just cant see it!

推薦答案

  1. 您將包含 i 的列表附加到 c,因此 i not in c始終返回 <代碼>正確.您應該單獨附加 i:c.append(i)
  1. You are appending a list containing i to c, so i not in c will always return True. You should append i on its own: c.append(i)

或者

  1. 只需使用集合(如果順序不重要):

  1. Simply use sets (if order is not important):

a = [1, 1, 2, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
c = set(a) & set(b)  #  & calculates the intersection.
print(c)
#  {1, 2, 3, 5, 8, 13}

編輯作為@Ev.Kounis 在評論中建議,您將通過使用獲得一些速度
c = set(a).intersection(b).

EDIT As @Ev. Kounis suggested in the comment, you will gain some speed by using
c = set(a).intersection(b).

這篇關于兩個列表之間沒有重復的公共元素的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
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(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 精品国产91 | 欧美另类视频 | 日韩成人免费视频 | 久草在线免费资源 | 欧美日韩精品中文字幕 | 亚洲欧美日韩电影 | 欧美性猛交一区二区三区精品 | 美女爽到呻吟久久久久 | 欧洲精品在线观看 | 亚洲成人www| 伊人网站视频 | 成人欧美一区二区三区在线播放 | 国产成人免费视频网站高清观看视频 | 亚洲视频免费观看 | 精品入口麻豆88视频 | 国产日韩精品视频 | 成人免费视频网站在线看 | 老牛嫩草一区二区三区av | 国产精品久久久久一区二区三区 | 韩国主播午夜大尺度福利 | 女同久久 | 亚洲手机视频在线 | 久久激情视频 | 色婷婷久久久久swag精品 | 中文字幕第100页 | 麻豆a级片 | 国产999精品久久久久久 | av网站免费看 | 一级高清免费毛片 | 国产精品久久免费观看 | 在线黄色影院 | 欧美精品三区 | 国产三级在线观看播放 | 九色91视频 | 日韩成人精品一区二区三区 | 色视频欧美 | 视频一区二区中文字幕 | www.99热 | 日本一区二区不卡视频 | 色综合久久天天综合网 | а_天堂中文最新版地址 |