0
I’m trying to make a function that returns the intersection between two sets:
def intersecao(conjuntoA, conjuntoB):
inter = [x for x in range(1,12) if x in conjuntoA and conjuntoB]
return inter
a = [2,4,6,7,10]
b = [3,4,6,8,10,11]
print(intersecao(a,b))
This function should return: [4,6,10] But it is returning: [2, 4, 6, 7, 10] I’ve tried it without understanding lists, but the result is the same... Would someone kindly point out where the mistake is? Because I’ve tried turning it into binary, and it still doesn’t work :/
You can simplify your code by eliminating the second
for
. Instead of going through the whole set B, you can just doif x in conjuntoB
. See: https://ideone.com/B8iGI7– Woss