python_集合
这周学的比较少,开始有点浑浑噩噩的感觉了
print() 里面共有两个默认值一个sep,end 一个代表分隔符,一个代表在尾部加入什么
print("hello","world",end="") print("hello","world",sep="和") print("hello","world") #hello world(#由于自带的\n被替换成空字符)hello和world #hello world
enumerate() 将列表的元素进行排序输出,(指定列表,从哪个数开始)
li = ["alec","aric","Tont",123] for iem , ele in enumerate(li,0): print(iem,ele) #0 alec 1 aric 2 Tont 3 123
集合
1.集合是由不同元素组成的
2.集合的目的是将不同的值存放到{}内
set() 将里面的字符串拆分出来,且无序,不会重复
a = set("hello") #{'o', 'l', 'h', 'e'}
.add 添加元素到集合
a = {"123","345",34,567,8} b = a.add("hello") print(b) #{'345', '123', 34, 8, 'hello', 567}无序
.remove() 删除指定元素,不存在报错
.discard 删除指定元素,不存在不报错
a = {"app","hahaha"} a.discard("hahaha") print(a) #{'app'}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
交集:求两个集合共同拥有的元素
a= {123,456,111} b = {123,456,789} c = a.intersection(b) print(c) #{456, 123}
#符号:集合1&集合2
并集:将两个集合里的元素合并到一起
a= {123,456,111} b = {123,456,789} c = a.union(b) print(c) #{789, 456, 123, 111}
符号:集合1|集合2
差集:两个集合中,你存在我不存在的元素
a= {123,456,111} b = {123,456,789} c = a.difference(b) print(c) #{111} #在a集合中存在而在b集合中不存在 符号:集合1-集合2
交叉补集:将两个合集去除共同部分并输出
a= {123,456,111} b = {123,456,789} c = a.symmetric_difference(b) print(c) #{789, 111} 符号:合集1^合集2
difference_update 从集合1中去除集合2
a= {123,456,111} b = {123,789} a.difference_update(b) print(a) #{456, 111}
isdisjoint 判断是否有共同元素,没有则返回TRue
a= {123,456,111} b = {789} c= a.isdisjoint(b) print(c) #TRue #因为b中没有存在a的共同元素所以输出True
子集:集合1是否为集合2的子集
issubset
a= {123,456,111} b = {123,456,111,125,156} c= a.issubset(b) print(c) #true
父集:集合1是否为集合2的父集
issuperset
a= {123,456,111} b = {123,456,111,125,156} c= b.issuperset(a) print(c) #TRue
#集合2<=集合1
update:将集合2添加到集合1中,集合1必须为集合,集合2可以为列表,元组
a= {23,456,111} b = ["hello",456,111,125,156] a.update(b) print(a) #{456, 'hello', 111, 23, 156, 125}
frozenset 去重复,可以为元组列表
v = ["hello",123,111,456,111,125,156] a = frozenset(v) print(a) #frozenset({'hello', 456, 111, 123, 156, 125})
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
百分号格式化输出
%s 代表可以接所有类型的元素
%d 只能接数字类型的元素
%f(%.2f) 格式化输出浮点型数字默认保留6位,后者保留2位
print("i have a %s"%(apple))
print("my age is %d"%(14))
print("your app is %.2f"%(2.12457))