标签:code inter col int nbsp diff intersect 列表 print
交集合
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))
标签:code inter col int nbsp diff intersect 列表 print
原文地址:https://www.cnblogs.com/wfl9310/p/8893833.html