分类:
可变不可变
可变:列表,字典
不可变:字符串,数字,元组
访问顺序
直接访问:数字
顺序访问:列表,字符串,元组
映射:字典
存放元素个数
容器类型:列表,元组,字典
原子:数字,字符串
集合:
1.不同元素组成
2.无序
3.元素必须是不可变类型(可哈希的值)
交集 intersection &
并集 union |
差集 difference -
交叉补集 symmetric_difference
方法:
#集合 s=set("hello") print(s) s=set(["alex","hello"]) print(s) # 结果 # {'l', 'h', 'e', 'o'} # {'hello', 'alex'}
# 添加add s={1,2,3,4,5,6} s.add(22) s.add(2) s.add('3') print(s) # 结果 # {1, 2, 3, 4, 5, 6, '3', 22} s={1,2,3,4,5,6} s.clear() print(s) # 结果 # set() # 删除,随机 s={1,2,3,4,5,6} s.pop() print(s) # 结果 # {2, 3, 4, 5, 6} # 指定删除,不存在时会报错 s={1,2,3,4,5,6} s.remove(2) print(s) # 结果 # {1, 3, 4, 5, 6} # 指定删除,不存在时不会报错 s={1,2,3,4,5,6} s.discard('22') print(s) # 结果 # {1, 2, 3, 4, 5, 6}
# 更新s1,更新多个值 s1={1,2} s2={3,5,43,1} print(s1.update(s2),s1,s2) # print(s1.union(s2),s1,s2)#不更新 # 结果 #None {1, 2, 3, 5, 43} {43, 3, 5, 1}
# 集合可以用来判断关系 python_1=["yangzi","siqi","chenglong","yunqianyue"] linux_1=["yangzi","daqiao","chenglong"] p_s=set(python_1) l_s=set(linux_1) print(p_s,l_s) # 交集 print(p_s.intersection(l_s)) print(p_s&l_s) # 结果 # {'chenglong', 'yangzi'} # 并集 print(p_s.union(l_s)) print(p_s|l_s) # 结果 # {'yunqianyue', 'yangzi', 'siqi', 'daqiao', 'chenglong'} # 差集 print(p_s.difference(l_s)) print(p_s-l_s) # 结果 # {'yunqianyue', 'siqi'} # 交叉补集 print(p_s.symmetric_difference(l_s)) # 结果 # {'yunqianyue', 'daqiao', 'siqi'}
python_1=["yangzi","siqi","chenglong","yunqianyue"] linux_1=["yangzi","daqiao","chenglong"] p_s=set(python_1) l_s=set(linux_1) print(p_s,l_s) # v=l_s.intersection_update(p_s) l_s.intersection_update(p_s) print(p_s,l_s) # 结果 # None {'yunqianyue', 'yangzi', 'siqi', 'chenglong'} {'yangzi', 'chenglong'}
# 是否有交集,否返回True s1={1,2} s2={3,5,43} print(s1.isdisjoint(s2)) # 结果 # True # s1是s2的子集,返回True s1={1,2} s2={3,5,43,1,2} print(s1.issubset(s2)) # 结果 # True
补充:
集合是可变的,可通过frozenset定义不可变集合
s=frozenset("hello") print(s)
# 简单去重 names=["yangzi","siqi","chenglong","yunqianyue","siqi"] s=list(set(names)) print(s)
#结果
#['yunqianyue', 'chenglong', 'yangzi', 'siqi']
字符串格式化
常规
%拼接,键方式
format用法
%s 字符串,接受所有
%d 数字,只能接受数字
%f 小数
补充:
sep拼接
# 字符串格式化 msg="i am "+"yang" print(msg) # 结果 # i am yang msg="i am %s hobby is %s"%('yang','swimming') print(msg) # 结果 # i am yang hobby is swimming msg="i am %s hobby is %d"%('yang',22) print(msg) # 结果 # i am yang hobby is 22
#打印浮点数
msg="i am %s hobby is %f"%('yang',22.2222223335678)
print(msg)
# # 结果
# # i am yang hobby is 22.222222
# 打印%
msg="i am %s hobby is %2f%%"%('yang',22.2222223335678)
print(msg)
# 结果
# i am yang hobby is 22.222222%
# 键方式 tp1="i am %(name)s hobby is %(bo)s"%{"name":"yang","bo":"Swim"} print(tp1) # 结果 # i am yang hobby is Swim
# 右对齐,颜色\033
tp1="i am \033[43:1m%(name)-20s hobby\033[0m is %(bo)s"%{"name":"yang","bo":"Swim"}
print(tp1)
# 结果
# i am yang hobby is Swim
# 拼接符 print("root","a","z",sep="-") # 结果 # root-a-z
format用法
# format # 一一对应 tp1="i am {},age {},hobby {}".format("yang",18,"swim") print(tp1) # 结果 # i am yang,age 18,hobby swim tp1="i am {2},age {0},hobby {1}".format("yang",18,"swim") print(tp1) # 结果 # i am swim,age yang,hobby 18
tp1="i am {name},age {a},hobby {h}".format(name="yang",a=15,h="swim") print(tp1) # 结果 # i am yang,age 15,hobby swim
tp1="i am {:s},age {:s},hobby {:s}".format(*["a","b","s",2],[222,"g"]) print(tp1) # 结果 # i am a,age b,hobby s tp1="i am {name},age {a},hobby {h}".format(**{"name":"yang","a":1,"h":"swim"}) print(tp1) # 结果 # i am yang,age 1,hobby swim # :b 二进制,:o八进制 tp1="{:b},{:o},{:d},{:x},{:X},{:%}".format(15,15,15,15,15,15.3334,33,"dd") print(tp1) # 结果 # 1111,17,15,f,F,1533.340000%
(视频8未看)