数据类型的回顾与总结
可变与不可变
1.可变:列表,字典
2.不可变:字符串,数字,元组
访问顺序:
1.直接访问:数字
2.顺序访问:字符串,列表,元祖
3.映射:字典
存放元素个数:
容器类型:列表,元祖,字典
原子:数字,字符串
set 集合
一、集合的基本属性
1.不同元素组成
2.无序
3.集合中元素必须是不可变类型(字符串,数字,元组)
s = {1,2,3,4,5,6} s = {1,2,3,3,3,3,3} print(type(s)) print(s) #<class 'set'> #{1, 2, 3}
定义方式
s = set('hello') print (s) #{'h', 'e', 'l', 'o'} s1 = set('hello') print(s1) s2 = set(['root','root','even']) print(s2) #{'e', 'o', 'l', 'h'} #{'even', 'root'}
set 集合 使用 for 循环里面的类型
如果有重复的值,则丢弃
二、集合的方法
1.add
添加,只能更新一个值,更新多个值使用 update
s = {1,2,3,4,5,6} s.add('s') print(s) #{1,2,3,4,5,6,'s'}
2.clear
清除
s = {1,2,3,4,5,6} s.clear() print(s) #set()
3.copy
拷贝值
s1 = {1,2,3,4,5,6} s2 = s1.copy() print(s2) #{1, 2, 3, 4, 5, 6}
4.pop
随机删除一个值,并保留该值
s = {'e',1,2,3,4,5,6} n = s.pop() print(s) print(n) #{2, 3, 4, 5, 6, 'e'} #1
5.remove
删除指定值,若删元素不存在,IDEA会报错
s = {'e',1,2,3,4,5,6} s.remove('e') print(s) #{1, 2, 3, 4, 5, 6} 注意: s = {'e',1,2,3,4,5,6} s.remove('root') #删除元素不存在,会报错 print(s)
6.discard
删除指定值,若要删除的元素不存在,IDEA不会报错
s = {'e',1,2,3,4,5,6} s.discard('root') #删除元素不存在,不会报错 print(s) #{1, 2, 3, 4, 5, 6, 'e'}
三、集合关系运算
现在有两个列表,求他们的交集
足球队人员是张三、李四、王五
篮球队人员是张三、王五、赵六
football = ['张三','李四','王五'] basketball = ['张三','王五','赵六'] #会踢足球的人员和会打篮球的人员 #如果不使用set集合的方式,使用list方式进行for循环,输出两门球类运动都会的人 all = [] #定义一个两门都会的空列表 for f in football: #将football内的元素for循环 if f in basketball: #如果这些元素也在basketball内 all.append(f) #添加这些元素进入[]空列表 print(all) #['张三', '王五']
1.intersection() 交集
通过 set的方法完成上面的要求
football = ['张三','李四','王五'] basketball = ['张三','王五','赵六'] #会踢足球的人员和会打篮球的人员 #使用set交集的方式,输出两门球类运动都会的人 f_s = set(football) #将football转换为f_s集合 b_s = set(basketball) #将basketball转换为b_s集合 print(f_s,b_s) print(f_s.intersection(b_s)) #取f_s和b_s的交集 #{'张三', '王五', '李四'} {'张三', '王五', '赵六'} #{'张三', '王五'}
2.union() 并集
football = ['张三','李四','王五'] basketball = ['张三','王五','赵六'] #会踢足球的人员和会打篮球的人员 #使用set并集的方式,输出两类总共人数 f_s = set(football) #将football转换为f_s集合 b_s = set(basketball) #将basketball转换为b_s集合 print(f_s,b_s) print(f_s.union(b_s)) #取f_s和b_s的交集 #{'王五', '张三', '李四'} {'赵六', '张三', '王五'} #{'赵六', '张三', '李四', '王五'}
3.difference()差集
football = ['张三','李四','王五'] basketball = ['张三','王五','赵六'] f_s = set(football) #将football转换为f_s集合 b_s = set(basketball) #将basketball转换为b_s集合 print('差集1',f_s - b_s) #f_s.difference(b_s) 等同于 f_s - b_s print(f_s.difference(b_s)) print('差集2',b_s - f_s) #b_s.difference(f_s) 等同于 b_s - f_s print(b_s.difference(f_s)) #差集1 {'李四'} #{'李四'} #差集2 {'赵六'} #{'赵六'}
4.symmetric_difference() 交叉补集
football = ['张三','李四','王五'] basketball = ['张三','王五','赵六'] f_s = set(football) #将football转换为f_s集合 b_s = set(basketball) #将basketball转换为b_s集合 print(f_s.symmetric_difference(b_s)) #f_s.difference(b_s) 等同于 f_s ^ b_s #{'李四', '赵六'}
PS:这几类方法后面加.update是求完值之后重新赋值
5.isdisjoint issubset issuperset
判断有无交集 判断是否是子集 判断是否是父集
6.update
更新多个值 和add不同的是 add只能添加单个值
s = {'s','g','h'} s.update('e','n',(10,1),[22,33],{50:2,30:4}) print(s) #字符串和所有可迭代对象都可以被update添加
#{1, 33, 'e', 10, 's', 50, 22, 'g', 'h', 30, 'n'}
字符串的格式化
一、常用格式化
%字符的用处
msg = 'i am %s my hobby is %s' % ('even','python') print(msg) #i am even my hobby is python
msg1 = 'i am %s my hobby is %s' % ('even',123) msg2 = 'i am %s my hobby is %s' % ('even',[1,2]) print(msg1) print(msg2) #i am even my hobby is 123 #i am even my hobby is [1,2]
打印浮点数
tpl = "percent %.2f" % 99.976238888 #.2f保留浮点数后两位 print(tpl) #percent 99.98
打印百分比
tpl = 'percent %.2f %%' % 99.976238888 print(tpl) #percent 99.98 %
:分隔符拼接
print('root','x','0','0',sep=':') #root:x:0:0
二、format 的常用格式化
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)
**代表字典对应格式化
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18}) print(tpl)
*代表列表对应格式化
tpl = "i am {:s}, age {:d}".format(*["seven", 18]) print(tpl)