相识 python 字典

Posted x-h-15029451788

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相识 python 字典相关的知识,希望对你有一定的参考价值。

字典————— dict

pyrhon的数据结构之一

字典:用于存储数据,存储大量数据。 字典要比列表快一些。将数据和数据之间进行关联,

dic = "key" : "value"————— 键值对

所有操作都是通过键

dic = "10":"苹果手机",

? "11":"苹果手机",

? 15:"小米手机",

? 15:"华为手机",
?
? (1,):"oppo手机",

?

print(dic)

键 : 必须是不可变的数据类型(可哈希),且唯一 不可嘻哈的数据类型是可变的类型

值:任意

字典是可变数据类型,无序的

names = ["日魔","炮手","豹哥"]

hy = ["看动漫","飞机","贴膏药"]

dic =

? "日魔":["看动漫","健身","吃包子","吃大煎饼","吃大烧饼"],

? "炮手":"飞机",

? "豹哥":"贴膏药",

? "宝元":"宝剑",

? "alex":"吹牛逼"

print(dic)

print(dic.get("日魔"))———— 查找

print(dic.get("炮手"))

1.暴力添加 :

dict ["小白"] = "曹阳" # ———— 字典的添加是一个键值对

dict ["小妹"] = ["小哥哥","大哥哥","小伙","帅哥","高富帅"]

print (dict)

2.有则不添加,无则添加

dic.setdefault("元宝",["唱","跳","篮球","喝酒"])—————— 只要键相同就不能添加

print(dic)

dic.setdefault("花哥哥","大保健")---------无则添加

print(dic)

setdefault分为两步:

1.先看键是否在字典

2.不存在的时候进行添加

删除

? dic =

? "日魔":["看动漫","健身","吃包子","吃大煎饼","吃大烧饼"],

? "炮手":"飞机",

? "豹哥":"贴膏药",

? "宝元":"宝剑",

? "alex":"吹牛逼"

?

pop

print (dict.pop("元宝"))----------pop删除通过字典中的渐近线删除 返回的被删除的内容

print(dict)

Clear

dic.clear() # 清空

? print(dic)

del

del dic ——————— 删除的整个容器

Print (dic)

del dic ["alex"]——————通过键进行删除

Print (dic)

字典中是没有remove *****

dic =

"日魔":["看动漫","健身","吃包子","吃大煎饼","吃大烧饼"],

"炮手":"飞机",

"豹哥":"贴膏药",

"宝元":"宝剑",

"alex":"吹牛逼"

dic ["alex"] = "dsb"——————— 有则覆盖 没有添加

print(dic)

? dic1 = "alex":"上过北大","wusir":"干过前端"

? dic1.update(dic)——————— 用dic中的内容更新dic1中的内容

? print(dic1)

dic =

"日魔":["看动漫","健身","吃包子","吃大煎饼","吃大烧饼"],

"炮手":"飞机",

"豹哥":"贴膏药",

"宝元":"宝剑",

"alex":"吹牛逼"

print(dic.get("alex"))—————— # 查询不到返回None

print(dic.get("元宝","找不到啊")) # 查找不到的时候返回自己制定的内容

print(dic.setdefault("alex")) # 查询不到返回None

print(dic["alex"]) # 查询不到就报错了

for i in dic :——————查看所有的键

? print (dic.get(i))

print (dic. keys()) 获取的是一个高仿列表

print (dic.values()) 获取到的是一个高仿列表

for i in dic.values():———————— 高仿列表支持迭代

? print(i)——————— 打印出来的是值

print(dic.values()[3]) 高仿列表不支持索引

lst = []

for i in dic:

lst.append(dic[i])

print(lst)

? print(list(dic.values()))

结果两个都是一样的—————— 数出来的都是值

lst = []

for i in dic:

? lst.append((i,dic[i]))

print(lst)------------- 输出的是键值对

print(list(dic.items()))———— 输出的是键值对

for i in dic:

print(i,dic[i])-----------
日魔 [‘看动漫‘, ‘健身‘, ‘吃包子‘, ‘吃大煎饼‘, ‘吃大烧饼‘]
炮手 飞机
豹哥 贴膏药
宝元 宝剑
alex 吹牛逼

for i in dic.items():

print(i[0],i[1])
日魔 [‘看动漫‘, ‘健身‘, ‘吃包子‘, ‘吃大煎饼‘, ‘吃大烧饼‘]
炮手 飞机
豹哥 贴膏药
宝元 宝剑
alex 吹牛逼

解构

a = 10

b = 20

a,b = b,a

print(a)

print(b)

a,b = 10,20

print(a)

print(b)

a = 10,20———————— a 是元祖

print(a)

print(10,20)

—————— (10, 20)
10 20

a,b = (1,20)

print(a)

print(b)

—————— 1
20

a,b = "wc"

print(a)
print(b)

————— w
c

dic = "key1":2,"key2":4—————— 迭代对象只在键不给值迭代解构的作用,

a,b = dic

print(a)

print(b)

—————— key1
key2

解构的作用,

lst = [1,2,3,4,5,6,7,8]

lst1 = lst[:2] # 两个都是列表的时候才能够相加

lst1.append(lst[4])

for i in lst1:

print(i)——————— 结果是1 , 2 , 5

print(lst[:2] + list(str(lst[4])))——— 两个都是列表的时候才能够相加

lst = [1,2,3,4,5,6,7,8]

a,b,c,d,e,aa = lst # 是万能接受

print(a,b,e)

for i in dic.items():

a,b = i

print(a)

print(b

for a,b in dic.items(): print(list(dic.items()))

print(a)
print(b)

嵌套

dic =

? 101:1:"日魔":"对象",
? 2:"隔壁老王":"王炸",
? 3:"乔碧萝":("日魔","炮手","宝元"),
? ,
? 102:1:"汪峰":"国际章":["小苹果","大鸭梨"],
? 2:"邓紫棋":["泡沫","信仰","天堂","光年之外"],
? 3:"腾格尔":["隐形的翅膀","卡路里","日不落"]
? ,
? 103:1:"蔡徐坤":"唱":["鸡你太美"],
? "跳":["钢管舞"],
? "rap":["大碗面"],
? "篮球":("NBA形象大使"),

? 2:"JJ":"行走的CD":["江南","曹操","背对背拥抱","小酒窝","不潮不花钱"],
? 3:"Jay":"周董":["菊花台","双节棍","霍元甲"],

? 201:
? 1:"韦小宝":"双儿":"刺客","建宁":"公主":"吴三桂":"熊","龙儿":"教主老婆":"教主"
?

print(dic[201][1]["韦小宝"]["建宁"]["公主"]["吴三桂"])

print(dic[103][1]["蔡徐坤"]["跳"][0][1])

print(dic[102][2]["邓紫棋"][1])

以上是关于相识 python 字典的主要内容,如果未能解决你的问题,请参考以下文章

Python初相识

我和python的初相识

简述我与python相识和理想

相识python 之小数据池 集合

Python入门-列表初相识

Python入门:元组初相识