473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1768
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...something
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(pair)
#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
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...
0
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...
3
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...
5
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....
10
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 ...
6
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',...
6
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...
15
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...
6
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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,...

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.