字典 dict
数据类型划分:可变数据类型,不可变数据类型
不可变数据类型: 元组,bool值,int,str 可哈希
可变数据类型: list,dict,set 不可哈希
dict key :必须是不可变数据类型,可哈希,
value:任意数据类型。
dict 优点:二分查找法去查询
存储大量的关系型数据
特点:无序的
字典 dict的操作
增
dic={'age': 18, 'name': 'jin', 'sex': 'male'} #直接改变dic['height'] = 180 # 原来没有的键值对,无序添加dic['age'] = 32 #原来有键,值直接覆盖改变# setdefault() dic.setdefault('height',180)# 没有才能添加成功dic.setdefault('name','john')#有则不改变任何dic.setdefault('hobbie') # 'hobbie': None
删
# pop() #删除存在的键值对dic.pop('age')# 成功print(dic.pop('age')) # 18 会返回删除的值 #删除不存在的,设置返回值提醒不存在dic.pop('height') #没有的键,删除会报错dic.pop('height',None)# 加上None 不会报错,返回noneprint(dic.pop('height','没有此件')) # 可设置返回值 #用于大量数据时删除不知道是否存在的值dic.pop('name',None) # 成功 {'age': 18, 'sex': 'male'}# popitem()print(dic.popitem()) # 随机删除 有返回值 返回 存在元组里面的 删除的键值。# deldel dic #删除字典del dic['name'] #删除键值对#clear()dic.clear() # 清空
改
#直接赋值dic['age'] = 32 #原来有键,值直接覆盖改变#updatedic2 = {'sex':'female','weight':75}dic2.update(dic) # 改变dic2 把后面的覆盖和添加到前面
查
在循环一个列表时,最好不要删除列表中的元素,这样会使索引发生改变,从而报错。
# print(dic.keys(),type(dic.keys()))#dict_keys(['age', 'sex', 'name']) <class 'dict_keys'>print(dic.values(),type(dic.values()))#dict_keys(['age', 'sex', 'name']) <class 'dict_keys'>print(dic.items(),type(dic.items()))#dict_items([('age', 18), ('name', 'jin'), ('sex', 'male')]) <class 'dict_items'>#循环去查for i in dic: # 仅返回键 同dic.keys()一样for i in dic.keys(): #返回键for i in dic.values():# 返回值print(i) #Python:找一一对应的最小单位for i in dic.items():print(i) # ('age', 18)('name', 'jin')('sex', 'male')for k,v in dic.items() :print(k,v) # name jin sex male age 18#直接赋值去查v1 = dic['name']print(v1)v2 = dic['name1'] # 报错print(v2)
# get()
print(dic.get('name1',"默认返回值") # 返回设定的返回值,默认返回none,防止报错
formkeys() 已知键,添加值。(可覆盖)
dic = dict.fromkeys([1,2,3],'春哥') print(dic) #{1: '春哥', 2: '春哥', 3: '春哥'} dic = dict.fromkeys([1,2,3],[]) print(dic) # {1: [], 2: [], 3: []} dic[1].append('袁姐') print(dic) # {1: ['袁姐'], 2: ['袁姐'], 3: ['袁姐']} dic[2].extend('二哥') print(dic) # {1: ['袁姐', '二', '哥'], 2: ['袁姐', '二', '哥'], 3: ['袁姐', '二', '哥']}
编码
1,各个编码之间的二进制,是不能互相识别的,会产生乱码。
2,文件的储存,传输,不能是unicode(只能是utf-8 utf-16 gbk,gb2312,asciid等)
asciiA : 00000010 8位 一个字节unicode A : 00000000 00000001 00000010 00000100 32位 四个字节中:00000000 00000001 00000010 00000110 32位 四个字节utf-8 A : 00100000 8位 一个字节中: 00000001 00000010 00000110 24位 三个字节gbk A : 00000110 8位 一个字节中 : 00000010 00000110 16位 两个字节
py3:str 在内存中是用unicode编码。bytes类型对于英文:str :表现形式:s = 'alex'编码方式: 010101010 unicodebytes :表现形式:s = b'alex'编码方式: 000101010 utf-8 gbk。。。。对于中文:str :表现形式:s = '中国'编码方式: 010101010 unicodebytes :表现形式:s = b'x\e91\e91\e01\e21\e31\e32'编码方式: 000101010 utf-8 gbk。。。。
s1 = 'alex' # encode 编码,如何将str --> bytes, ()s11 = s1.encode('utf-8')s11 = s1.encode('gbk')print(s11)s2 = '中国's22 = s2.encode('utf-8')s22 = s2.encode('gbk')print(s22)
中文一定要用什么编码就用什么解码,英文则不必。
# str --->byte encode 编码s = '二哥'b = s.encode('utf-8')print(b)#byte --->str decode 解码s1 = b.decode('utf-8')print(s1)
集合
集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的。以下是集合最重要的两点:
去重,把一个列表变成集合,就自动去重了。
关系测试,测试两组数据之前的交集、差集、并集等关系。
集合的创建
set1 = set({1,2,'barry'})set2 = {1,2,'barry'}print(set1,set2) # {1, 2, 'barry'} {1, 2, 'barry'}
集合的操作


set1 = {'alex','wusir','ritian','egon','barry'}set1.add('景女神')print(set1)#update:迭代着增加set1.update('A')print(set1)set1.update('老师')print(set1)set1.update([1,2,3])print(set1)


set1 = {'alex','wusir','ritian','egon','barry'}set1.remove('alex') # 删除一个元素print(set1)set1.pop() # 随机删除一个元素print(set1)set1.clear() # 清空集合print(set1)del set1 # 删除集合print(set1)


for i in set1:print(i)
集合的其他操作
交集。(& 或者 intersection)


set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 & set2) # {4, 5} print(set1.intersection(set2)) # {4, 5}
并集。(| 或者 union)


set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7}print(set2.union(set1)) # {1, 2, 3, 4, 5, 6, 7}
差集。(- 或者 difference)


set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 - set2) # {1, 2, 3} print(set1.difference(set2)) # {1, 2, 3}
反交集。 (^ 或者 symmetric_difference)


set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 ^ set2) # {1, 2, 3, 6, 7, 8} print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7, 8}
子集与超集


set1 = {1,2,3} set2 = {1,2,3,4,5,6}print(set1 < set2) print(set1.issubset(set2)) # 这两个相同,都是说明set1是set2子集。print(set2 > set1) print(set2.issuperset(set1)) # 这两个相同,都是说明set2是set1超集。
frozenset不可变集合,让集合变成不可变类型。
s = frozenset('barry') print(s,type(s)) # frozenset({'a', 'y', 'b', 'r'}) <class 'frozenset'>
for i in s:
print(i)
# y
# r
# a
# b
深浅copy
1 赋值运算 对于赋值运算来说,l1与l2指向的是同一个内存地址,所以他们是完全一样的。
l1 = [1,2,3,['barry','alex']] l2 = l1l1[0] = 111 print(l1) # [111, 2, 3, ['barry', 'alex']] print(l2) # [111, 2, 3, ['barry', 'alex']] l1[3][0] = 'wusir' print(l1) # [111, 2, 3, ['wusir', 'alex']] print(l2) # [111, 2, 3, ['wusir', 'alex']] 复制代码
2 浅拷贝copy


l1 = [1,2,3,['barry','alex']]l2 = l1.copy() print(l1,id(l1)) # [1, 2, 3, ['barry', 'alex']] 2380296895816 print(l2,id(l2)) # [1, 2, 3, ['barry', 'alex']] 2380296895048 l1[1] = 222 print(l1,id(l1)) # [1, 222, 3, ['barry', 'alex']] 2593038941128 print(l2,id(l2)) # [1, 2, 3, ['barry', 'alex']] 2593038941896 l1[3][0] = 'wusir' print(l1,id(l1[3])) # [1, 2, 3, ['wusir', 'alex']] 1732315659016 print(l2,id(l2[3])) # [1, 2, 3, ['wusir', 'alex']] 1732315659016
对于浅copy来说,第一层创建的是新的内存地址,而从第二层开始,指向的都是同一个内存地址,所以,对于第二层以及更深的层数来说,保持一致性。
3 深拷贝deepcopy


import copy l1 = [1,2,3,['barry','alex']] l2 = copy.deepcopy(l1)print(l1,id(l1)) # [1, 2, 3, ['barry', 'alex']] 2915377167816 print(l2,id(l2)) # [1, 2, 3, ['barry', 'alex']] 2915377167048 l1[1] = 222 print(l1,id(l1)) # [1, 222, 3, ['barry', 'alex']] 2915377167816 print(l2,id(l2)) # [1, 2, 3, ['barry', 'alex']] 2915377167048 l1[3][0] = 'wusir' print(l1,id(l1[3])) # [1, 222, 3, ['wusir', 'alex']] 2915377167240 print(l2,id(l2[3])) # [1, 2, 3, ['barry', 'alex']] 2915377167304
对于深copy来说,两个是完全独立的,改变任意一个的任何元素(无论多少层),另一个绝对不改变
4 文件操作
文件名.txt
1,文件路径:d:\文件名.txt
2,编码方式:utf-8 gbk 。。。。
3,操作方式:只读,只写,追加,读写,写读.....
以什么编码方式储存的文件,就以什么编码打开进行操作。


rrbf = open('文件名',mode='r',encoding='utf-8')content = f.read()print(content,type(content))f.close()f = open('文件名',mode='rb',)content = f.read()print(content)f.close()r+ 读写r+b 读写(以bytes类型)f = open('log',mode='r+',encoding='utf-8')print(f.read())f.write('大猛,小孟')f.close()f = open('log',mode='r+b')print(f.read())f.write('大猛,小孟'.encode('utf-8'))f.close()


f = open('log',mode='a',encoding='utf-8')f.write('佳琪')f.close()f = open('log',mode='ab')f.write('佳琪'.encode('utf-8'))f.close()


w #对于w:没有此文件就会创建文件wb # 先将源文件的内容全部清除,在写。 先将源文件的内容全部清除,在写。f = open('log',mode='w',encoding='utf-8')f.write('附近看到类似纠纷')f.close()f = open('log',mode='wb')f.write('附近看到类似纠纷'.encode('utf-8'))f.close()w+f = open('log',mode='w+',encoding='utf-8')f.write('aaa')f.seek(0)print(f.read())f.close()w+b.......


f = open('log',mode='r+',encoding='utf-8') content = f.read(3) # 读出来的都是字符 f.seek(3) # 是按照字节定光标的位置 f.tell() #告诉你光标的位置 print(f.tell())f.readable() # 是否可读 f.truncate(4)# 再源文件上截取打印 line = f.readline() # 一行一行的读 line = f.readlines() # 每一行当成列表中的一个元素,添加到list中 for line in f:print(line)
# f = open('log',mode='a+',encoding='utf-8') # f.write('佳琪') # count = f.tell() # f.seek(count-9) # print(f.read(2)) # f.close()