Reputation: 1
I created a code that compares between sets, when I intersect between two sets, I want to check if they are the same. However the last line of code that does the check returns false, does anyone know why?
x = ['bomb', 'lock', 'clock']
y = ['bomb', 'lock', 'clock']
def cards_intersect(card1, card2):
card1 = set(card1) #turning cards into sets
card2 = set(card2)
return card1.intersection(card2) # return intersection of sets
print(x)
print(cards_intersect(x, y))
print(cards_intersect(x, y) == x) #why false?
Upvotes: 0
Views: 85
Reputation: 11
cards_intersect(x, y)
and x has different type.
type(cards_intersect(x, y))
is set
, type(x)
is list
.
It is like the difference between 1
and '1'
Upvotes: 1
Reputation: 15364
As you can see from your print
statements
['bomb', 'lock', 'clock']
{'bomb', 'lock', 'clock'}
x
and cards_intersect(x, y)
are different. The first one is a list, the second one is a set. You may want to compare two sets:
print(set(x) == cards_intersect(x,y)) # True
Upvotes: 1
Reputation:
The cards_intersect
function only changes card1, card2 inside the function's scope. Therefore when you are comparing cards_intersect(x, y)
which returns a set, to the list x
- you get false
.
You can compare in the following way:
print(cards_intersect(x, y) == set(x))
Upvotes: 0