what:数据类型可以按照多种角度进行分类,就跟咱们人一样,人按照地域可以划分分为亚洲人,欧洲人,美洲人等,但是按照肤色又可以分为白种人,黄种人,黑种人,等等,数据类型可以按照不同的角度进行分类,先给大家按照可变与不可变的数据类型的分类:
不可变(可哈希)的数据类型:int,str,bool,tuple。
可变(不可哈希)的数据类型:list,dict,set。
字典是Python语言中的映射类型,他是以{}括起来,里面的内容是以键值对的形式储存的:
Key: 不可变(可哈希)的数据类型.并且键是唯一的,不重复的。
Value:任意数据(int,str,bool,tuple,list,dict,set),包括后面要学的实例对象等。
在Python3.5版本(包括此版本)之前,字典是无序的。
在Python3.6版本之后,字典会按照初建字典时的顺序排列(即第一次插入数据的顺序排序)。
当然,字典也有缺点:他的缺点就是内存消耗巨大。但是字典的查询速度是十分快的。
创建字典
# 创建字典的几种方式: # 方式1: dic = dict(((\'one\', 1),(\'two\', 2),(\'three\', 3))) # dic = dict([(\'one\', 1),(\'two\', 2),(\'three\', 3)]) print(dic) # {\'one\': 1, \'two\': 2, \'three\': 3} # 方式2: dic = dict(one=1,two=2,three=3) print(dic) # {\'one\': 1, \'two\': 2, \'three\': 3} # 方式3: dic = dict({\'one\': 1, \'two\': 2, \'three\': 3}) print(dic) # {\'one\': 1, \'two\': 2, \'three\': 3} # 方式5: 后面会讲到先了解 dic = dict(zip([\'one\', \'two\', \'three\'],[1, 2, 3])) print(dic) # 方式6: 字典推导式 dic = { k: v for k,v in [(\'one\', 1),(\'two\', 2),(\'three\', 3)]} print(dic) # 方式7:利用fromkey dic = dict.fromkeys(\'abcd\',\'zhouqian\') print(dic) # {\'a\': \'zhouqian\', \'b\': \'zhouqian\', \'c\': \'zhouqian\', \'d\': \'zhouqian\'} dic = dict.fromkeys([1, 2, 3],\'zhouqian\') print(dic) # {1: \'zhouqian\', 2: \'zhouqian\', 3: \'zhouqian\'}
验证字典的合法性
# 合法 dic = {123: 456, True: 999, "id": 1, "name": \'sylar\', "age": 18, "stu": [\'帅 哥\', \'美⼥\'], (1, 2, 3): \'麻花藤\'} print(dic[123]) print(dic[True]) print(dic[\'id\']) print(dic[\'stu\']) print(dic[(1, 2, 3)]) # 不合法 # dic = {[1, 2, 3]: \'周杰伦\'} # list是可变的. 不能作为key # dic = {{1: 2}: "哈哈哈"} # dict是可变的. 不能作为key dic = {{1, 2, 3}: \'呵呵呵\'} # set是可变的, 不能作为key
字典的增删改查
字典的增
# 通过键值对直接增加 dic = {\'name\': \'zhouqian\', \'age\': 18} dic[\'weight\'] = 75 # 没有weight这个键,就增加键值对 print(dic) # {\'name\': \'zhouqian\', \'age\': 18, \'weight\': 75} dic[\'name\'] = \'barry\' # 有name这个键,就成了字典的改值 print(dic) # {\'name\': \'barry\', \'age\': 18, \'weight\': 75} # setdefault dic = {\'name\': \'zhouqian\', \'age\': 18} dic.setdefault(\'height\',175) # 没有height此键,则添加 print(dic) # {\'name\': \'zhouqian\', \'age\': 18, \'height\': 175} dic.setdefault(\'name\',\'barry\') # 有此键则不变 print(dic) # {\'name\': \'zhouqian\', \'age\': 18, \'height\': 175} #它有返回值 dic = {\'name\': \'zhouqian\', \'age\': 18} ret = dic.setdefault(\'name\') print(ret) # zhouqian
字典的删
# pop 通过key删除字典的键值对,有返回值,可设置返回值。 dic = {\'name\': \'zhouqian\', \'age\': 18} ret = dic.pop(\'name\') print(ret,dic) # zhouqian {\'age\': 18} ret1 = dic.pop(\'n\',None) print(ret1,dic) # None {\'name\': \'zhouqian\', \'age\': 18} #clear 清空字典 dic = {\'name\': \'zhouqian\', \'age\': 18} dic.clear() print(dic) # {} # del # 通过键删除键值对 dic = {\'name\': \'zhouqian\', \'age\': 18} del dic[\'name\'] print(dic) # {\'age\': 18} #删除整个字典 del dic
字典的改
# 通过键值对直接改 dic = {\'name\': \'zhouqian\', \'age\': 18} dic[\'name\'] = \'barry\' print(dic) # {\'name\': \'barry\', \'age\': 18} # update dic = {\'name\': \'zhouqian\', \'age\': 18} dic.update(sex=\'男\', height=175) print(dic) # {\'name\': \'zhouqian\', \'age\': 18, \'sex\': \'男\', \'height\': 175} dic = {\'name\': \'zhouqian\', \'age\': 18} dic.update([(1, \'a\'),(2, \'b\'),(3, \'c\'),(4, \'d\')]) print(dic) # {\'name\': \'zhouqian\', \'age\': 18, 1: \'a\', 2: \'b\', 3: \'c\', 4: \'d\'} dic1 = {"name":"jin","age":18,"sex":"male"} dic2 = {"name":"alex","weight":75} dic1.update(dic2) print(dic1) # {\'name\': \'alex\', \'age\': 18, \'sex\': \'male\', \'weight\': 75} print(dic2) # {\'name\': \'alex\', \'weight\': 75}
字典的查
# 通过键查询 # 直接dic[key](没有此键会报错) dic = {\'name\': \'zhouqian\', \'age\': 18} print(dic[\'name\']) # zhouqian # get dic = {\'name\': \'zhouqian\', \'age\': 18} v = dic.get(\'name\') print(v) # \'zhouqian\' v = dic.get(\'name1\') print(v) # None v = dic.get(\'name2\',\'没有此键\') print(v) # 没有此键 keys() dic = {\'name\': \'zhouqian\', \'age\': 18} print(dic.keys()) # dict_keys([\'name\', \'age\']) values() dic = {\'name\': \'zhouqian\', \'age\': 18} print(dic.values()) # dict_values([\'zhouqian\', 18]) items() dic = {\'name\': \'zhouqian\', \'age\': 18} print(dic.items()) # dict_items([(\'name\', \'zhouqian\'), (\'age\', 18)])