首页 > 其他 > 详细

交集、并集、差集

时间:2018-04-20 22:29:48      阅读:168      评论:0      收藏:0      [点我收藏+]
交集合
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))

 

交集、并集、差集

原文:https://www.cnblogs.com/wfl9310/p/8893833.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!