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

兩個列表的交集,在第一個列表中保留重復項

Intersection of two lists, keeping duplicates in the first list(兩個列表的交集,在第一個列表中保留重復項)
本文介紹了兩個列表的交集,在第一個列表中保留重復項的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有兩個平面列表,其中一個包含重復值.例如,

I have two flat lists where one of them contains duplicate values. For example,

array1 = [1,4,4,7,10,10,10,15,16,17,18,20]
array2 = [4,6,7,8,9,10]

我需要在array1 中找到也在array2 中的值,將重復項保留在array1 中.期望的結果將是

I need to find values in array1 that are also in array2, KEEPING THE DUPLICATES in array1. Desired outcome will be

result = [4,4,7,10,10,10]

我想避免循環,因為實際數組將包含數百萬個值.我嘗試了各種集合和相交組合,但就是無法保留重復項..

I want to avoid loops as actual arrays will contain over millions of values. I have tried various set and intersect combinations, but just couldn't keep the duplicates..

推薦答案

你不想使用循環是什么意思?您將不得不以一種或另一種方式對其進行迭代.只需單獨接收每個項目并檢查它是否在 array2 中:

What do you mean you don't want to use loops? You're going to have to iterate over it one way or another. Just take in each item individually and check if it's in array2 as you go:

items = set(array2)
found = [i for i in array1 if i in items]

<小時>

此外,根據您將如何使用結果,考慮使用生成器:


Furthermore, depending on how you are going to use the result, consider having a generator:

found = (i for i in array1 if i in array2)

這樣您就不必一次將整個內容全部記住.

so that you won't have to have the whole thing in memory all at once.

這篇關于兩個列表的交集,在第一個列表中保留重復項的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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精品综合久久久久久五月天 | 久久国产综合 | 欧美激情综合 | 精久久久| 国产精品久久久久久一区二区三区 | 欧美精品一区二区三区蜜桃视频 | 国产高清视频在线观看 | 久久另类 | 免费成人高清 | 一级片视频免费观看 | 99久久婷婷国产精品综合 | 91精品国产日韩91久久久久久 | 欧美综合一区二区 | 综合久久综合久久 | 欧美一区二区三区在线播放 | 亚洲视频一区在线观看 | 中文字幕在线视频精品 | 国产免费人成xvideos视频 | 国产网站在线免费观看 | 国产精品黄视频 | 欧美婷婷| 日韩欧美一区二区在线播放 | 国产福利二区 | 国产精品日韩欧美一区二区三区 | 欧美日韩在线成人 | 中文成人无字幕乱码精品 | 久久久久久99 | 91网在线播放 | 久久小视频| 午夜影院黄 | 国产精品a免费一区久久电影 | 久久精品亚洲 | 日本亚洲欧美 | 亚洲永久| 日韩一区二区在线免费观看 | 作爱视频免费看 |