可变数据类型与不可变数据类型:
1.可变:列表,字典
2.不可变:字符串,数字,元组
访问顺序:
1.顺序访问:字符串,列表,元组
2.映射:字典
集合
由不同元素组成的集合,集合中是一组无序排列的可hash的值,可以作为字典的key
1.不同元素组成
2.无序
3.集合中元素必须是不可变类型,如:字符串,数字,元组
集合的定义:s={1,2,5,6,7}
s = {0,1,2,4,5,6,7,7,7,8,9}for i in s : print(i) v={(1,2,3,),5} for i in v : print(i)
>>>
0
1
9
7
>>>
5
(1, 2, 3)
set定义:相当于for循环,()里为可迭代类型
s=set("heloor") print(s) >>>{‘e‘, ‘h‘, ‘r‘, ‘l‘, ‘o‘}
基本方法
添加:add()
清空:clear()
拷贝:copy()
删除:pop(),指定删除:remove(value)删除元素不存在时会报错,discard(value)删除元素不存在时不会报错
求交、差、并集:
python_li=[‘lcg‘,‘szw‘,‘zjw‘,‘lcg‘] linux_li=[‘lcg‘,‘szw‘] python_and_linux=[] #for循环求交集 for i in python_li: if i in linux_li: python_and_linux.append(i) print(python_and_linux) #集合求交集 p_s=set(python_li) l_s=set(linux_li) print(p_s, l_s) print(p_s.intersection(l_s)) print(p_s&l_s) #求并集 print(p_s.union(l_s)) print(p_s|l_s) #求差集,我中有,你中没有的部分 print(p_s-l_s) print(l_s-p_s) print(p_s.difference(l_s))
#求交叉补集,两个集合求并集后减去交叉的部分 print(p_s.symmetric_difference(l_s)) print(p_s^l_s)
python_li=[‘lcg‘,‘szw‘,‘zjw‘,‘lcg‘,‘asdr‘,‘fdf‘] linux_li=[‘lcg‘,‘szw‘,‘sffr‘,‘roy‘] p_s=set(python_li) l_s=set(linux_li) #求差集后将值更新为p_s p_s.difference_update(l_s) print(p_s)
#判断交集,如果没有交集返回True b=p_s.isdisjoint(l_s) print(b)
#判断是否是子集 print(p_s.issubset(l_s))
#判断是否是父集 print(p_s.issuperset(l_s)) #更新,覆盖,可更新多个值,add只能更新一个值 p_s.update(‘a‘) print(p_s)
补充:
集合是可变类型,能增加,能删除,但不可修改,
另一种集合定义方式:
s=frozenset(‘hello‘),此种方式定义的集合为不可变类型
简单去重复操作:
l=[‘zel‘,‘zel‘,‘komma‘] name=list(set(l)) print(name)
字符串格式化:
#%s可以接收字符串,或任何类型 msg=‘i am %s my hobby is ball‘%‘manuel‘ print(msg) m1=‘you are so %s,%s is not a good habit‘%(‘cute‘,‘ball‘) print(m1) #%d只能接收数字 m2=‘age:%d‘%18 print(m2) #%f打印浮点数,.4代表保留小数点后4位 f=‘percent%.4f‘%999.12345678 print(f) #打印百分号,用两个百分号 p=‘percent%.3f%%‘%54.157894 print(p) #用键的方式拼接字符串 t3="i am %(name)s ,age:%(age)d"%{‘name‘:‘Manuel‘,‘age‘:18} print(t3)
1、百分号方式
%[(name)][flags][width].[precision]typecode
注:Python中百分号格式化是不存在自动将整数转换成二进制表示的方式
2.format方式
2、Format方式
[[fill]align][sign][#][0][width][,][.precision][type]
常用格式化:
tpl = "i am {}, age {}, {}".format("seven", 18, ‘alex‘) tpl = "i am {}, age {}, {}".format(*["seven", 18, ‘alex‘]) tpl = "i am {0}, age {1}, really {0}".format("seven", 18) tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18]) tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18) tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18}) tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33]) tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1) tpl = "i am {:s}, age {:d}".format(*["seven", 18]) tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18) tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2) tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15) tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
原文:https://www.cnblogs.com/Manuel/p/10518056.html