集合 set
1.不同元素组成
2.无序
3.集合中元素必须是不可变类型
s = {1,2,3,4,5}
集合常用魔法
s = {1,2,3,4,5}
s.add('s')
print(s) #>>>{1, 2, 3, 4, 5, 's'}
s.add(6)
print(s) #>>>{1, 2, 3, 4, 5, 's', 6}
s.clear()
print(s)
s = {1,2,3,4,5}
v = s.copy()
print(v)
s = {1,2,3,4,5}
s.pop() #随机删除一个元素 因为集合是无序的所以不能指定删除某个元素
print(s)
s = {1,2,3,4,5}
s.remove(4) #删除指定元素,不能删除不存在的元素,会报错
print(s)
s = {1,2,3,4,5}
s.discard(4) #同样是删除指定元素,但是当删除不存在的元素时不会报错,用的比较多
print(s)
集合关系运算
交集 intersection()
s1 =['lg','hg','lbj']
s2 =['lg','hg','gk']
s3 = set(s1)
s4 = set(s2)
s5 = s3.intersection(s4) #交集
print(s5)#{'lg', 'hg'}
print(s3&s4) #也是交集
并集 union()
print(s3.union(s4)) #并集 {'gk', 'lg', 'hg', 'lbj'}
print(s3|s4)#并集
差集 difference()
print(s3.difference(s4))#差集 求出s3有,而s4没有的 >>>{'lbj'} 不会改变原来的s3
print(s3-s4) #>>>{'lbj'}
s3.difference_update(s4) #将s3与s4交集的结果放入s3中 ,会改变原来的s3
print(s3)##>>>{'gk'}
交叉补集 symmetric_difference()
print(s3.symmetric_difference(s4)) #先把两个并在一块,然后去除共有的部分
print(s3^s4) #>>>{'lbj', 'gk'}
update()
#===update 可以更新多个值,而add只能更新一个值
s1.update(s2) #s1变s2
print(s1)
s1 = {1,2}
s2 = {1,2,3,4,5}
s1.update((3,4)) #s1.update([3,4]) 也可以
print(s1)#>>>>{1, 2, 3, 4}
其他
#==isdisjoint()
print(s3.isdisjoint(s4)) #如果两个集合没有交集,返回True,有返回False
#====
s1 = {1,2}
s2 = {1,2,3,4,5}
print(s1.issubset(s2)) #s1是s2的子集则返回True ,否则返回False >>>True
print(s2.issubset(s1))#>>>False
print(s1.issuperset(s2)) ##s1是s2的父集则返回True ,否则返回False >>>False
print(s2.issuperset(s1))#>>>True
#===frozenset 创建不可变集合 而我们平时的set是可变的
s = frozenset("hello")
print(s) #>>>frozenset({'l', 'e', 'o', 'h'})
百分号字符串拼接
1、百分号方式
%[(name)][flags][width].[precision]typecode
(name) 可选,用于选择指定的key
flags 可选,可供选择的值有:
" + 右对齐;正数前加正好,负数前加负号;"
" - 左对齐;正数前无符号,负数前加负号;"
空格 右对齐;正数前加空格,负数前加负号;
0 右对齐;正数前无符号,负数前加负号;用0填充空白处
width 可选,占有宽度
.precision 可选,小数点后保留的位数
typecode 必选
lg = "i am %s ,my hoppy is %s" %("hg" ,"baskeball")
print(lg)#>>>i am hg ,my hoppy is baskeball
lg = "i am %s ,my hoppy is %s" %("hg" ,11)
print(lg)#>>>i am hg ,my hoppy is 11
lg = "i am %s ,my hoppy is %s" %("hg" ,[22]) #%s可以接受任何数字类型
print(lg)#>>>i am hg ,my hoppy is [22]
lg = "i am %(name)s ,my age is %(age)s" %{"name":"hg","age":22} #打印字典
print(lg) #>>>i am hg ,my age is 22
lg = " i am %s ,my hoppy is %d" %("hg" ,1) #%d只能接受数字,其他的都不行
d = "date: %.2f" % 90.1111 #打印浮点数 .代表截取位数
print(d) #>>>date: 90.11
#打印百分比
d = "date: %.2f %%" % 90.1111 #
print(d)
lg = "i am %(name)-20s ,my age is %(age)s" %{"name":"hg","age":22} #打印字典
print(lg) #>>>i am hg ,my age is 22
lg = "i am %(name)+20s ,my age is %(age)s" %{"name":"hg","age":22} #打印字典
print(lg) #>>>i am hg ,my age is 22
print('root','user','Desktop',sep='/') #>>>root/user/Desktop
print('root','user','Desktop',sep=':') #>>>root:user:Desktop
format格式化
tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
print(tpl) #>>>i am seven, age 18, alex
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex']) #*号可以取列表里的元素填入,如果没有*号会报错
print(tpl)#>>>i am seven, age 18, alex
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) #
print(tpl)#>>>i am seven, age 18, really seven
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) #转字典i am seven, age 18, really seven
print(tpl)
tpl = "i am {0[0]}, age {0[1]}, really {1[2]}".format([1, 2, 3], [11, 22, 33])#前一个序号代表取第几个列表,第二个代表取列表里的值
print(tpl) #>>>i am 1, age 2, really 33
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) #:s表示取得是字符串 :d取的是数字 :f 取浮点型
print(tpl) #>>>i am seven, age 18, money 88888.100000
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623,2) #%,显示百分比(默认显示小数点后6位)
print(tpl)#numbers: 1111,17,15,f,F, 1587.623000%
标签:lg,s3,函数,Python,age,am,print,集合,hg
From: https://www.cnblogs.com/huanggai/p/18304187