两个列表的交集,在第一个列表中保留重复项

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 检测图像中矩形的中心和角度)