Python No.18_集合以及表示符号
# !/usr/bin/env python# -*- coding:utf-8 -*-# Author:Hugolinhj list_1 = [1,4,5,7,6,3,7,9]list_1 = set(list_1)print(list_1,type(list_1))#list_1转化为集合数据类型,集合的特点,特点:排除重复。集合也是无序的。 list_2 = set([2,6,3,4,33,41,8])print(list_1,list_2) ##############################例################################# list_3 = list_1.intersection(list_2)print(list_3)#两个集合的交集 list_3 = list_1.union(list_2)print(list_3)#两个集合的并集 list_3 = list_1.difference(list_2)print(list_3)#差集,list1里面有,但list2没有 list_3 = list_1.issubset(list_2)print(list_3)#判断list1是否为list2得子集 list_3 = list_1.issuperset(list_2)print(list_3)#判断list1是否为list2得父集 list_3 = list_1.symmetric_difference(list_2)print(list_3)#判断list1、list2中,互相没有的取出来。对称差集 list_3 = set([1,3,4])list_4 = set([5,6,8])list_5 = list_3.isdisjoint(list_4)print(list_5)#判断list3、list4中,没有出现交集,则True。 print(list_1)list_3 = list_1.pop()print('1>>',list_3)list_3 = list_1.pop()print('2>>',list_3)list_3 = list_1.pop()print('3>>',list_3)list_3 = list_1.pop()print('4>>',list_3)list_3 = list_1.pop()print('5>>',list_3)print('6>>',list_1)#随机删除一个值,并反馈删除的值 list_2 = set([2,6,3,4,33,41,8])print('==>',list_2.discard('11'))print('==>>',list_2.discard(2))print('==>>>',list_2)#删除指定一个值,不反馈删除值,即使删除错误也不反馈。
list_1.add(999)print(list_1)#添加一项 list_1.update([2,999,333])print(list_1)#添加多项
#采用符号的常用操作: s = set ([3,5,9,10])#创建一个数值集合t = set (['hello'])#创建一个唯一字符的集合r = set ([3,'hesl']) print(s)print(t)print(r)a = t | s | r#t和s的并集b = t & s#t和s的交集c = t - s#求差集(项在t中,不在s中)d = t ^ s#求对称差集(项在t或者s中,但不会同事出现在二者中) print('1.',a)print('2.',b)print('3.',c)print('4.',d) s = set([3,3,5,4])s.remove(4)print(s)#删除 y=len(s)print(y)#s的长度 s = set ([3,5,9,10])t = set (['hello']) ss = 3 in sprint(ss)#判断3是否在s集合中 tt = 'hel' in tprint(tt)#字符串无法切片去判断 ss = 3 not in sprint(ss)#判断3是否不在s集合中 r = set([1,3,4,5,6,78,9,10])rr = s <= rprint(rr)#判断s是否为r的子集 rr = s >= rprint(rr)#判断s是否为r的父集 p = ([1,1,3,[3,4,5,5],1])pp = p.copy()print(pp)#返回p的浅复制
赞 (0)