I'm very new to Python so please be kind!:) I'm just messing about at the moment but I can't find anything specific on this particular issue.
I'm looking to remove duplicates from an output based upon the the sum of two numbers. I have the following lists of numbers and the short code where I can print the numbers that multiply to give 24.
A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8
for a in A:
for b in B:
if (a * b) == 24:
print (a, b, a+b)
The outcomes here are:
3 8 11
4 6 10
6 4 10
8 3 11
In the output there are four combinations that give 24, two of which are permutations of each other. Is there any way that I can deduplicate the permutations to get "unique" outputs? One of the ways that I thought to do this was by adding a+b
and trying to remove the duplicates in output based on this number but I can't think of a way to do this. Anyone got any ideas how I could do this?
Create a set and store combinations of (a,b)
, (b,a)
in it if printing. Don't print if (a,b)
already in set:
A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8
got = set() # collect (a,b) and (b,a) that you printed already to avoid dupes
for a in A:
for b in B:
if (a * b) == 24:
if (a,b) in got:
continue
got.add((a,b)) # need to add (a,b) and (b,a) to avoid printing
got.add((b,a)) # (b,a) after you already printed (a,b)
print (a, b, a+b)
Output:
3 8 11
4 6 10
Lets compare this and olivn s approach - I removed both print statements:
A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8
def ol():
res = set()
for a in A:
for b in B:
if a * b == 24:
if {a, b} not in res:
# print(a, b, a + b)
res.add(frozenset((a, b)))
def pa():
got = set() # collect (a,b) and (b,a) that you printed already to avoid dupes
for a in A:
for b in B:
if (a * b) == 24:
if (a,b) in got:
continue
got.add((a,b)) # need to add (a,b) and (b,a) to avoid printing
got.add((b,a)) # (b,a) after you already printed (a,b)
# print (a, b, a+b)
from timeit import timeit
print("Olivn:", timeit(ol, number=10000))
print("this: ", timeit(pa, number=10000))
Output:
Olivn: 0.60943335
this: 0.6066992629999999
You may create keep track of the numbers with results as 24 and can avoid the duplicate numbers as follow
A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8
pass_list: list = []
for a in A:
for b in B:
if (a * b) == 24 and b not in pass_list:
pass_list.append(a)
print (a, b, a+b)
Output
3 8 11
4 6 10
Just extending @patrick's comparison with 1000000 counts, this answer seems bit more faster.
Olivn: 9.081725899999999
Patrick: 7.241247899999999
this: 5.5779484
Olivn: 9.3896246
Patrick: 7.960243800000001
this: 5.6804922
Olivn: 10.6548058
Patrick: 8.282727000000001
this: 7.1877061
Olivn: 11.1385039
Patrick: 9.226674899999999
this: 8.4106494
A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8
def ol():
res = set()
for a in A:
for b in B:
if a * b == 24:
if {a, b} not in res:
# print(a, b, a + b)
res.add(frozenset((a, b)))
def pa():
got = set() # collect (a,b) and (b,a) that you printed already to avoid dupes
for a in A:
for b in B:
if (a * b) == 24:
if (a,b) in got:
continue
got.add((a,b)) # need to add (a,b) and (b,a) to avoid printing
got.add((b,a)) # (b,a) after you already printed (a,b)
# print (a, b, a+b)
def pr():
pass_list: list = []
for a in A:
for b in B:
if (a * b) == 24 and b not in pass_list:
pass_list.append(a)
#print (a, b, a+b)
from timeit import timeit
print("Olivn:", timeit(ol, number=1000000))
print("Patrick: ", timeit(pa, number=1000000))
print("this: ", timeit(pr, number=1000000))
@PatrickArtner decided to not follow my recommendations in comments under his answer so here is correct (imho) way how you can apply set
(and frozenset
as well) :
A = 1,2,3,4,5,6,8
B = 1,2,3,4,5,6,8
res = set()
for a in A:
for b in B:
if a * b == 24:
if {a, b} not in res:
print(a, b, a + b)
res.add(frozenset((a, b)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.