交集、并集、差集
交集合
cat=['fish','beef','pig'] dog=['beef','pig'] #列表转变为集合 cat1=set(cat) dog1=set(dog) print(cat1,dog1) 输出{'pig', 'beef', 'fish'} {'pig', 'beef'} print(cat1.intersection(dog1)) 输出{'pig', 'beef'} print(cat1&dog1) 输出{'pig', 'beef'}
并集
cat=['fish','beef','pig'] dog=['beef','pig'] #列表转变为集合 cat1=set(cat) dog1=set(dog) print(cat1.union(dog1)) 输出 {'fish', 'pig', 'beef'} print(cat1|dog1) 输出{'fish', 'pig', 'beef'}
差集
cat=['fish','beef','pig'] dog=['beef','pig'] #列表转变为集合 cat1=set(cat) dog1=set(dog) print(cat1-dog1) print(cat1.difference(dog1))