码迷,mamicode.com
首页 > 其他好文 > 详细

集合[1]

时间:2017-07-25 12:33:30      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:[1]   class   style   int   false   去重   span   code   logs   

1 #集合去重
2 
3 list_1 = [1,2,3,3,4,4,5,6,7,7]
4 list_1 = set(list_1)
5 print(list_1,type(list_1))

交集 intersection()

1 #交集
2 
3 list_2 = set([2,4,6,7,9,7])
4 
5 list_3 = list_1.intersection(list_2)
6 
7 print(list_3)

并集 union()

1 #并集
2 list_4 = list_1.union(list_2)
3 print(并集\n,list_4)

差集difference()

1 #差集
2 list_5 = list_1.difference(list_2)
3 list_6 = list_2.difference(list_1)
4 print(差集 \n %s \n %s %(list_5,list_6))

子集issubset()  父集issuperset()

1 #子集 or 父集
2 list_7 = list_1.issubset(list_2) #list_1的子集是不是list_2
3 print(list_7)
4 list_8 = list_2.issuperset(list_1)#list_2的父集是不是list_1
5 print(list_8)

判断无交集 是True 否False

1 #无交集
2 list_10 = set([8,9,10])
3 list_11 = list_1.isdisjoint(list_10)
4 print(list_11)  #True 

 

集合[1]

标签:[1]   class   style   int   false   去重   span   code   logs   

原文地址:http://www.cnblogs.com/msl23/p/7233322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!