字典(dict)----------------------------------------------------------------------存储大量数据
-
{}"键":"值" 在别的语言中叫做键值对数据
-
dic = {"key":"value",1:2,2:3}
-
键:必须是可哈希(不可变的数据类型),并且是唯一的
-
值:任意
-
字典是无序的,python3.6版本以上 默认了咱们定义顺序,python3.5以下是随机显示
-
字典是可变的,增加,删除,修改,查看 其他操作
-
dic = {"key":1,1:123,False:True,(1,2,3):"abc",1:[1,3,4]}
print(dic)
-
字典怎么用
dic={"a":"b","b":"c"}
-
增
dic["键"] = "值" dic.setdefault("键","值") # 键不存在就添加,存在就不填加. 1.dic.setdefault("c","aaa") print(dic) 2.dic.setdefault("a","aaa") print(dic) 3.print(dic.setdefault("D"))先去字典查看咱们要添加的键存不存在,如果键在字典 中存在就不填加,如果要是不存在就添加 4.dic["s"] = "ssss" # dic[键] = 值 print(dic)
-
删
dic = {"a":"b","b":"c"} 1.print(dic.pop("a")) #通过键删除 pop具有返回值,返回的被删除的值 print(dic) 2.字典没有remove 3.del dic # 删除整个字典 print(dic) 4.del dic["b"] #删除指定的键值对 print(dic) 5.dic.clear() # 清空 print(dic) # 删除整个字典中的键值对
-
改
dic1 = {"a":"b","C":"c"} 1.dic["b"] = "这是值" # 键在字典中存在就是修改,键在字典中不存在就是增加 print(dic) 2.dic2 = {1:2,"a":"a1"} dic2.update(dic1) # update括号里的字典级别高,会被前面的括号中相同的key对应的值覆盖,没有就添加 print(dic2)
-
查(字典的查询只能通过键获取值,不能通过值获取键)
dic = {1:2,"a":"a1"} 1.print(dic["c"]) # 如果有键在字典中存在就放回键对应的值,如果不存在就报错 2.get -- 获取 print(dic.get("c","你不是傻,你个狍子")) 获取值的时候可以指定返回的内容,键不存在的时候不报错,返回None dic = {"a":1} dic["a"] += 5 dic.get("a") print(dic) 3.其它方法 for循环 dic = {1:2,"a":"a1"} for i in dic: # for循环字典的时候打印的是字典的键 print(i,dic[i]) for i in dic.keys(): print(i) dic.keys() -- 返回的是一个高仿列表 -- 没有索引 dic.values() -- 返回的是一个高仿列表 -- 没有索引 dic.items() -- 返回的是一个高仿列表,列表里存放的是元组类型,元组第一个 是键,第二个元素是值 for k,v in dic.items(): print(k,v)
-
练习题
dic = {"1":1,"2":2,"3":3,"4":4} 删除"2" 2种方式 dic.pop("2") del dic["2"] 添加 "5":5 2种方式 dic.setdefault("5",5) dic["5"] = 5 修改: "1":10086 dic["1"] = 10086 查看"3"的值 dic.get("3",'没有值') dic["3"] # 没有就会报错 dic = {"1":1,"2":2,"3":3,"4":4} li = [] for key in dic: li.append(key) print(li) for i in dic.keys():#高仿列表 print(i) print(type(dic.keys())) 可以for循环,但是不能索引 dic_li = dic.keys() print(dic_li) dic = {"1":1,"2":2,"3":3,"4":4} print(dic.values()) for i in dic.items(): print(i)
-
解构
a = 1 b = 2 print("is a",id(a))a,b的内存 print("is b",id(b)) a,b = b,a-------------交换的是指向 print("is a",id(a)) print("is b",id(b)) a,_,c = "你好啊" print(a,c)你啊 a,b = ("1","2") print(a,b) a,b = ["4","5"] print(a,b) a,b = {"a":1,"b":2,}-------------------------字典拆包后的结果是键赋值给 了a和b的变量 print(a,b) dic = {"1":1,"2":2,"3":3,"4":4} for key,value in dic.items(): print(key,value) keys,values,items,解构 dic = {"1":1,"2":2,"3":3,"4":4} for i in dic: # 效率高 print(i,dic[i]) for k,v in dic.items(): print(k,v)
-
字典是无序的
dic = {"电脑":15000,"手机":8000,"耳机":4000,"老婆":1111} 1 电脑 2 手机 3 耳机 4 老婆 1.for i in range(len(dic)): print(i+1) 2.count = 1 for i in dic: print(count,i) count += 1 3.li = [1,2,3,4,54] for i,k in enumerate(li): # enumerate 第二个参数默认不写就是0 -- 枚举 print(i,k)
-
字典的嵌套():
dic = {101:{1:["周杰伦","林俊杰"],2:{"汪峰":["国际章",{"前妻1":["熊大","熊二"]},{"前妻2":["葫芦娃","木吒"]}]}},102:{1:["李小龙","吴京","李连杰"],2:{"谢霆锋":["张柏芝","王菲"]},3:["alex","wusir","大象","奇奇"]}, 103:{1:["郭美美","干爹"],2:{"王宝强":{"马蓉":"宋哲"}}},201:{1:["凹凸曼","皮卡丘"],2:{"朱怼怼":{"杨幂":"刘恺威"}}}} print(dic[101][2]["汪峰"][2]["前妻2"][0]) ['国际章', {'前妻1': ['熊大', '熊二']}, {'前妻2': ['葫芦娃', '木吒']}] {'前妻2': ['葫芦娃', '木吒']} ['葫芦娃', '木吒'] home1 = dic[102][3][2] {1: ['李小龙', '吴京', '李连杰'],2: {'谢霆锋': ['张柏芝', '王菲']},3: ['alex', 'wusir', '大象', '奇奇']} print(home1) print(dic[103][2]["王宝强"]["马蓉"])
-
-
字典能干什么
- 字典是存储大量的数据,字典比列表还有大
- 字典在查找值的时候能够方便,快速
- li = ["a","b","c","d"]
- 字典能够将数据进行关联
- dic = {"a":"b","b":"c","c":"d"}
- 字典比较消耗内存 最常用的数据 : 字符串,列表,字典
- 有明确的对应关系时,推荐使用字典 字典一定要用明白 -- json
- list((1,3,4)) -- 列表 tuple([1,2,3]) -- 元组
- s = "{'a':1,'b':2}"
print(dict(s)) 目前所学的知识点字典不能转换的