473,804 Members | 3,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

remove matching pairs

Is there a simple way to to identify and remove matching pairs from 2
lists?

For example:

I have

a=[2, 5, 3, 4, 7, 2, 2, 4, 8, 1]
b=[7, 3, 5, 8, 1, 7, 4, 8, 2, 6]

and I want to get this:

a=[2, 5, 3, 4, 7, 2, 8, 1]
b=[7, 3, 5, 8, 1, 4, 2, 6]

There are recurring pairs of (2, 7) and (4, 8), and so I want to remove
one of each pair.

Many thanks, Evan

Dec 14 '06 #1
2 1788
Is there a simple way to to identify and remove matching pairs from 2
lists?

For example:

I have

a=[2, 5, 3, 4, 7, 2, 2, 4, 8, 1]
b=[7, 3, 5, 8, 1, 7, 4, 8, 2, 6]

and I want to get this:

a=[2, 5, 3, 4, 7, 2, 8, 1]
b=[7, 3, 5, 8, 1, 4, 2, 6]
Well, with a few caveats, the following works:
>>a=[2, 5, 3, 4, 7, 2, 2, 4, 8, 1]
b=[7, 3, 5, 8, 1, 7, 4, 8, 2, 6]
a_prime, b_prime = zip(*set(zip(a, b)))
a_prime
(2, 8, 4, 7, 1, 5, 2, 3)
>>b_prime
(7, 2, 8, 1, 6, 3, 4, 5)

Caveat #1: the order changed because sets are unordered
Caveat #2: a_prime and b_prime are tuples, not lists

If this isn't a true solution (because either #1 or #2 is an
unacceptable condition), you'd have to go with a loop...somethin g
like this untested

pairs = zip(a,b)
uniq = set(pairs)
a_prime = []
b_prime = []
for pair in pairs:
if pair in uniq:
a_prime.append( pair[0])
b_prime.append( pair[1])
uniq.remove(pai r)
#if not uniq: break

This should preserve the order as well as maintain listsrather
than return tuples. Depending on the number of duplicates you
expect and the size of your a/b lists, uncommenting out that last
line may give you a short speedup, as if you've already pulled
all the items out the uniq set, there's no reason to continue
iterating over the list of pairs.

HTH,

-tkc

Dec 14 '06 #2
That's great, thank you, the caveats are no matter!

-Evan

Dec 14 '06 #3

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

Similar topics

17
14078
by: Andrew McLean | last post by:
I have a problem that is suspect isn't unusual and I'm looking to see if there is any code available to help. I've Googled without success. Basically, I have two databases containing lists of postal addresses and need to look for matching addresses in the two databases. More precisely, for each address in database A I want to find a single matching address in database B. I'm 90% of the way there, in the sense that I have a simplistic...
0
1651
by: aredo3604gif | last post by:
I have coded a serie of singly linked lists in ANSI C which I have to use. The lists are then stored in a serie of buckets with chained hash table technique. In the various lists there are nodes that carry the same type of data in some of their node fields and some don't. In the various nodes , the struct datatype, there is a flag set to 1 for items that I need to remove. In the removing condition I have to add another match with an...
3
1822
by: Girish Sahani | last post by:
Hi, I am trying to convert a list of pairs (l4) to list l5 by removing those pairs from l4 which are not present in a third list called pairList. The following is a simplified part of the routine i have written. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. def getl5(): l5 = pairList = ,,,,,,,,]
5
1321
by: Girish Sahani | last post by:
Hi, I am trying to modify a list of pairs (l4) by removing those pairs which are not present in a third list called pairList. The following is a simplified part of the routine i have written. However it does not give the correct output. Please help! Its possible i have made a trivial mistke since i am a newbie. def getl5(): l5 = pairList = ,,,,,,,,]
10
1889
by: bullockbefriending bard | last post by:
first, regex part: I am new to regexes and have come up with the following expression: ((1|),(1|)/){5}(1|),(1|) to exactly match strings which look like this: 1,2/3,4/5,6/7,8/9,10/11,12 i.e. 6 comma-delimited pairs of integer numbers separated by the
6
3843
by: lisong | last post by:
Hi All, I have problem to split a string like this: 'abc.defg.hij.klmnop' and I want to get all substrings with only one '.' in mid. so the output I expect is : 'abc.defg', 'defg.hij', 'hij.klmnop'
6
6425
by: Daniel Fetchinson | last post by:
Hi all, There are a number of free tools for image matching but it's not very easy to decipher the actual algorithm from the code that includes db management, GUI, etc, etc. I have my own image database and GUI so all I need is the actual algorithm preferably in pseudo code and not in the form of a research paper (from which I also found a lot but since I'm not that much interested in the actual science of image recognition this seems...
15
2453
by: mcjason | last post by:
I saw something interesting about a grid pair puzzle problem that it looks like a machine when you find each that work out the way it does and say with all the others that work out the way they do together overlapping common pieces but say connected each working out as connected, but together as connected it's connected with the others connected. a whole machine where connected together is a condition of the machine together as...
6
4258
by: falconsx23 | last post by:
I am trying to write a code for a Phone Directory program. This program is suppose to allow the user to enter a name or directory and then program can either add, save or even delete an entry. Also this program has more then one class and also uses an interface. Right now I am working on ArrayBasedPD class. I am trying to write a code for the remove method (line 158) that allows the user to enter a name, once the program sees that the name is...
0
10562
Oralloy
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...
1
10303
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,...
0
10070
tracyyun
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...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
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...
0
6845
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();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4282
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
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.