(编辑:jimmy 日期: 2024/11/12 浏览:2)
集合是无序的,天生不重复的数据组合,它的作用如下:
> 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}
> 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
> name_1 = [1,2,3,4,7,8,7,10] > name_1 = set(name_1) #结果输出 > 1 in name_1 True
> name_1 = [1,2,3,4,7,8,7,10] > name_1 = set(name_1) #输出 > 12 not in name_1 True
更多关于Python集合操作方法可查看下面的相关链接