473,696 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bisect intersection


I stumbled into a sorted list intersection algorithm by Baeza-Yates
which I found quite elegant. For the lucky enough to have a springerlink
access, here's the citation:

I implemented this algorithm in python and I thought I could share it.
I've done some tests and, of course, it can't compete against dict/set
intersection, but it will perform pretty well. Computing union and
differences are left as an exercise...

from bisect import bisect_left

def bisect_intersec t(L1, L2):
inter = []
def rec(lo1, hi1, lo2, hi2):
if hi1 <= lo1: return
if hi2 <= lo2: return
mid1 = (lo1 + hi1) // 2
x1 = L1[mid1]
mid2 = bisect_left(L2, x1, lo=lo2, hi=hi2)
rec(lo1, mid1, lo2, mid2)
if mid2 < hi2 and x1 == L2[mid2]:
inter.append(x1 )
rec(mid1+1, hi1, mid2+1, hi2)
rec(mid1+1, hi1, mid2, hi2)
rec(0, len(L1), 0, len(L2))
return inter
Jun 27 '08 #1
0 1208

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Gary Robinson | last post by:
I'm wondering if bisect.insort is guaranteed to use a __cmp__ method, for now and for future python versions, if it exists, for the items being inserted? That would be good to know because then you don't have to define __lt__ etc. if all you care about is being able to use bisect.insort. If true, I suggest that it might be good to mention the bisect library docs. In any case, if there is a clear answer to my question I'd appreciate...
by: Antoine Logean | last post by:
Hi, What is the easiest way to get the intersection of two strings in python (a kind a "and" operator) ? ex: string_1 = "the_car_of_my_fried_is_bigger_as_mine_but_my_girlfriend_is_more_beautifull" string_2 = "my_girlfriend_is_more_beautifull_and_has_blue_eyes"
by: Gordon Williams | last post by:
Hi, I have to lists that I need to find the common numbers (2nd rounded to nearest integral) and I am wondering if there is a more efficient way of doing it. >>> a= >>> b= >>> (l,round(m))]
by: Sebastian Cohen S | last post by:
Hello, I am new to SQL and currently using Access 2003 and need a little help on the following. I have two queries each contains one column formed with a string that is very similar (only variying by few characters) to the other column as follows: QUERY1 QUERY2
by: kimos | last post by:
hi all, how to calculate the intersection of 2 rectangle a rectangle is the following: Rectangle makeRectangle (Point lowerLeft, Point upperRight) { Rectangle r;
by: SA Trygubenko | last post by:
Dear All, Python 2.4 (#1, Mar 22 2005, 21:42:42) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Queue >>> q=Queue.Queue() >>> type(q.queue) <type 'collections.deque'> >>>
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: s1.intersection(s2) I want the item matching to be a bit "looser".. that is, items in s2 that match to just the beginning of items in s1 would be included in the result of intersection().
by: Paulo da Silva | last post by:
Hi! What is the best way to have something like the bisect_left method on a list of lists being the comparision based on an specified arbitrary i_th element of each list element? Is there, for lists, something equivalent to the __cmp__ function for classes? Thanks.
by: Gabriel Genellina | last post by:
En Mon, 28 Apr 2008 13:50:13 -0300, Robert Bossy <Robert.Bossy@jouy.inra.frescribió: Nice. As it only uses comparisons, it can be used even if the elements aren't hashable, that's good. But if they are hashable, using a set and sorting again at the end is much faster, as you discovered. -- Gabriel Genellina
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.