Python中如何操作集合?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
>>> name_1 = [1,2,3,4,7,8,7,10] #把列表转换为集合 >>> name_1 = set(name_1) #转换后,去重 >>> print(name_1,type(name_1)) {1, 2, 3, 4, 7, 8, 10} <class 'set'>
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出结果 >>> name_1.intersection(name_2) {8, 1, 10, 3}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出结果 >>> name_1.union(name_2) {1, 2, 3, 4, 5, 7, 8, 10}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出结果 >>> name_1.difference(name_2) {2, 4, 7}
判断一个集合是否是另一个集合的子集
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #输出结果 >>> name_3.issubset(name_1) True
判断一个集合是否是另一个集合的父集
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #输出结果 >>> name_1.issuperset(name_3) True
把两个集合没有交集的数值取出来
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出结果 >>> name_1.symmetric_difference(name_2) {2, 4, 5, 7}
判断两个集合是否有交集,没有交集,则返回True
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_3 = [11] >>> name_1 = set(name_1) >>> name_2 = set(name_2) >>> name_3 = set(name_3) #有交集 >>> name_1.isdisjoint(name_2) False #无交集 >>> name_1.isdisjoint(name_3) True
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #结果输出 >>> name_1 & name_2 {8, 1, 10, 3}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #结果输出 >>> name_1 | name_2 {1, 2, 3, 4, 5, 7, 8, 10}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #结果输出 >>> name_1 - name_2 {2, 4, 7}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出 >>> name_1 ^ name_2 {2, 4, 5, 7}
>>> name_1 = [1,2,3,4,7,8,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #输出 >>> name_3 <= name_1 True
>>> name_1 = [1,2,3,4,7,8,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #输出 >>> name_1 >= name_3 True
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) #添加已存在,不报错 >>> name_2.add(1) >>> name_2 {8, 1, 10, 3, 5} #添加不存在,添加一个新的数值 >>> name_2.add(11) >>> name_2 {1, 3, 5, 8, 10, 11}
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2.update([12,13,14]) #输出结果 >>> name_2 {1, 3, 5, 8, 10, 12, 13, 14}
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2 {8, 1, 10, 3, 5} >>> name_2.remove(1) #输出 >>> name_2 {8, 10, 3, 5} #删除不存在的元素,会报错 >>> name_2.remove(1) Traceback (most recent call last): File "<input>", line 1, in <module> KeyError: 1
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2 {8, 1, 10, 3, 5} #输出 >>> name_2.pop() 8
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2.discard(10) #输出结果 >>> name_2 {8, 1, 3, 5} #删除不存在元素,不报错 >>> name_2.discard(10)
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #结果输出 >>> len(name_1) 7
测试 x 是否是 s 的成员
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #结果输出 >>> 1 in name_1 True
测试 x 是否不是 s 的成员
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #输出 >>> 12 not in name_1 True
1、云计算,典型应用OpenStack。2、WEB前端开发,众多大型网站均为Python开发。3.人工智能应用,基于大数据分析和深度学习而发展出来的人工智能本质上已经无法离开python。4、系统运维工程项目,自动化运维的标配就是python+Django/flask。5、金融理财分析,量化交易,金融分析。6、大数据分析。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。