list intersection algorithm implementation only using python lists (not sets)

2024/9/20 7:17:00

I've been trying to write down a list intersection algorithm in python that takes care of repetitions. I'm a newbie to python and programming so forgive me if this sounds inefficient, but I couldn't come up with anything else. Here, L1 and L2 are the two lists in question, and L is the intersection set.

  1. Iterate through L1
  2. Iterate through L2
  3. If element is in L1 and in L2
  4. add it to L
  5. remove it from L1 and L2
  6. iterate through L
  7. add elements back to L1 and L2

I'm 100% sure this is not the algorithm used within Mathematica to evaluate list intersection, but I can't really come up with anything more efficient. I don't want to modify L1 and L2 in the process, hence me adding back the intersection to both lists. Any ideas? I don't want to make use of any built in functions/data types other than lists, so no import sets or anything like that. This is an algorithmic and implementation exercise, not a programming one, as far as I'm concerned.

Answer

Here is a faster solution:

def intersect_sorted(a1, a2):"""Yields the intersection of sorted lists a1 and a2, without deduplication.Execution time is O(min(lo + hi, lo * log(hi))), where lo == min(len(a1),len(a2)) and hi == max(len(a1), len(a2)). It can be faster depending onthe data."""import bisect, maths1, s2 = len(a1), len(a2)i1 = i2 = 0if s1 and s1 + s2 > min(s1, s2) * math.log(max(s1, s2)) * 1.4426950408889634:bi = bisect.bisect_leftwhile i1 < s1 and i2 < s2:v1, v2 = a1[i1], a2[i2]if v1 == v2:yield v1i1 += 1i2 += 1elif v1 < v2:i1 = bi(a1, v2, i1)else:i2 = bi(a2, v1, i2)else:  # The linear solution is faster.while i1 < s1 and i2 < s2:v1, v2 = a1[i1], a2[i2]if v1 == v2:yield v1i1 += 1i2 += 1elif v1 < v2:i1 += 1else:i2 += 1

It runs in O(min(n + m, n * log(m))) time where n is the minimum of the lengths and m is the maximum. It iterates over both lists at the same time, skipping as many elements in the beginning as possible.

An analysis is available here: http://ptspts.blogspot.ch/2015/11/how-to-compute-intersection-of-two.html

https://en.xdnf.cn/q/119359.html

Related Q&A

In keras\tensorflow, How adding CNN layers to last layer of ResNet50V2 that pre-train on imagenet

I am trying to drop the last layer and add a simple CNN instead like the following, model = Sequential() base_model = ResNet50V2(include_top=False, weights="imagenet", input_shape=input_shape…

How to get missing date in columns using python pandas [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

vigenere cipher - not adding correct values

I want to get specific values from a for loop to add to another string to create a vigenere cipher.heres the code.userinput = input(enter message) keyword = input(enter keyword) new = for a in keyword…

Why isnt my output returning as expected?

So I wrote this code def diagsDownRight(M):n = len(M)m = [[] * (n - i - 1) + row + [] * i for i, row in enumerate(M)]return ([.join(col) for col in zip(*m)]), [.join(col[::-1]) for col in zip(*m)] def …

Django Stripe payment does not respond after clicking the Submit Payment button

I have an e-commerce application that Im working on. The app is currently hosted on Heroku free account. At the moment I can select a product, add it on the cart and can get up to the stripe form and t…

get file path using backslash (\) in windows in python [duplicate]

This question already has answers here:How can I put an actual backslash in a string literal (not use it for an escape sequence)?(4 answers)Closed 2 years ago.How to get result exactly the same format…

Printing progress bar on a console without the use of for -loop

I have a script written in python, where I have a statement:Process.open() //some parametersWhich executes a command and puts the output on the console ,where I do not know the time taken to execute t…

ModuleNotFoundError: No module named verovio

Hi there I would like to run my flask app in a container but I got stucked caused of a third party module. (I am using PyCharm)This is my docker file:FROM python:3-alpineMAINTAINER fooCOPY app /appWORK…

Python: TypeError: list object is not callable on global variable

I am currently in the process of programming a text-based adventure in Python as a learning exercise. I want "help" to be a global command, stored as values in a list, that can be called at (…

Python beautifulsoup how to get the line after href

I have this piece of html:<a href="http://francetv.fr/videos/alcaline_l_instant_,12163184.html" class="ss-titre">"Paris Combo" </a> <…