For example:
fav_tv = [{'The Simpsons', 'Modern Family'}, {'Friends', 'The Simpsons'}, {'The
Simpsons', 'Friends', 'Seinfeld'}, {'The Simpsons', 'Seinfeld'}]
output = 1
because 'The Simpsons' is the only show that appears in all sets
CodePudding user response:
Just use the intersection
:
len(fav_tv[0].intersection(*fav_tv[1:]))
CodePudding user response:
You can have that with a simple oneliner:
print(len(fav_tv[0].intersection(fav_tv[1], fav_tv[2], fav_tv[3])))
CodePudding user response:
with naive approach
>>> from collections import defaultdict as dd
>>> result = dd(int)
>>>
>>> fav_tv = [{'The Simpsons', 'Modern Family'}, {'Friends', 'The Simpsons'}, {'The Simpsons', 'Friends', 'Seinfeld'}, {'The Simpsons', 'Seinfeld'}]
>>>
>>> for i in fav_tv:
... for j in i:
... result[j] =1
...
>>>
>>>
>>> result
defaultdict(<class 'int'>, {'Modern Family': 1, 'The Simpsons': 4, 'Friends': 2, 'Seinfeld': 2})
>>>
>>>
>>> sum(1 for k, v in result.items() if v==len(fav_tv))
1