ardelean bogdan
ardelean bogdan

Reputation: 33

Python : comparing 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

Upvotes: 3

Views: 193

Answers (1)

zs2020
zs2020

Reputation: 54514

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)

Upvotes: 4

Related Questions