I'm trying to compare 2 sets of data
set1 = [a,b,c,d,e,f]
set2 = [a,c,e]
How would I go about finding out which variable from set2 are in set1 then output this to another set showing the results?
Use intersection:
set2.intersection(set1)
Or set(set2).intersection(set1) if you actually have a list.
Based on your comment maybe a dict where the values are booleans based on whether each element from set1 is in the intersection set:
set1 = ["a","b","c","d","e","f"]
set2 = ["a","c","e"]
inter = set(set2).intersection(set1)
vals = {k:k in inter for k in set1}
print(vals)
{'a': True, 'c': True, 'b': False, 'e': True, 'd': False, 'f': False}
If all you want is a mapping then maybe creating a set from set2 is sufficient:
set1 = ["a","b","c","d","e","f"]
set2 = ["a","c","e"]
st2 = set(set2)
vals = {k: k in st2 for k in set1}
Or to get two sets, for the common and different elements:
st2 = set(set2)
inter = st2.intersection(set1)
diff = st2.difference(set1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With