Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : comparing tuples

Tags:

python

tuples

I am trying to compare tuple A values with tuple B values , and make a 3rd tuple with common values. This is my code so far. Any attepts i have made to get a 3rd tuple with common values failed. Any help is apreciated.

#1st nr , print divs
x = int(raw_input('x=' ))
divizori = ()
for i in range(1,x):
    if x%i == 0:
        divizori = divizori + (i,)
print divizori
#2nd nr , print divs
y = int(raw_input('y=' ))
div = ()
for i in range(1,y):
    if y%i == 0:
        div = div + (i,)
print div
#code atempt to print commom found divs
like image 252
ardelean bogdan Avatar asked Aug 26 '26 09:08

ardelean bogdan


1 Answers

You can take advantage of set operations:

>>> a = (1,2,3,4)
>>> b = (2,3,4,5)
>>> tuple(set(a).intersection(set(b)))
(2, 3, 4)
like image 70
zs2020 Avatar answered Aug 28 '26 22:08

zs2020



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!