字典是python里面唯一的映射類型,由一個個鍵值對組成。
- 字典的創(chuàng)建
- 字典的查詢
- 字典的刪除
- 字典的修改or添加
- 字典的內(nèi)置方法(BIF)
- 字典的特性
- 通訊錄練習(xí)
一、創(chuàng)建字典(two)
-
使用{}創(chuàng)建
-
使用dict()函數(shù)
demo
>>
>
dict1
=
{
'one'
:
1
,
'two'
:
2
,
'three'
:
3
}
>>
>
dict2
=
dict
(
one
=
1
,
two
=
2
,
three
=
3
)
>>
>
print
(
dict1
[
'one'
]
)
>>
>
print
(
dict2
[
"one"
]
)
1
1
上面的demo中需要注意的是 dict()方法創(chuàng)建字典的時候無需key無需加"",這個函數(shù)會自動給key加"" ,如果你加上它會報錯。
二、查詢字典
-
dict
訪問整個字典,字典會無序的返回,因為字典重視的是key和value之間的對應(yīng)關(guān)系,而非排序。 -
dict1[“key”]:
通過訪問key,返回value,
print(dict[“one”]) -
dict1.get(“key”,“報錯內(nèi)容”):
如果字典中不存在某個key,而你訪問的話會報錯,這個時候使用get方法比較合適
如果存在key,會直接返回key對應(yīng)的value,如果不存在返回你設(shè)置的報錯內(nèi)容。
demo
>>
>
dict2
=
dict
(
one
=
1
,
two
=
2
,
three
=
3
)
>>
>
print
(
dict2
)
{
'one'
:
1
,
'two'
:
2
,
'three'
:
3
}
##這是巧合,一般會返回?zé)o序的字典
>>
>
print
(
dict2
[
"one"
]
)
1
>>
>
print
(
dict2
.
get
(
"one"
,
"不存在"
)
)
#雖然get后面有報錯內(nèi)容,但鍵存在所以不會顯示
1
>>
>
print
(
dict2
.
get
(
"five"
,
"不存在"
)
)
#five這個鍵不存在,所以會顯示報錯內(nèi)容,給用戶良好的交互感。
不存在
三、刪除字典
-
del dict1:
刪除整個字典 -
dict1.clear()
刪除整個字典 -
del dict[“key”]
刪除字典中的鍵-值對 - dict.pop(“key”)
四、修改or添加字典
dict[“key”] = value:
如果key存在,則修改value,如果key不存在,則添加value
demo
>>
>
dict2
=
dict
(
one
=
1
,
two
=
2
,
three
=
3
)
>>
>
dict2
[
"two"
]
=
22
>>
>
dict2
[
"four"
]
=
4
>>
>
print
(
dict2
)
{
'one'
:
1
,
'two'
:
22
,
'three'
:
3
,
'four'
:
4
}
五、字典的內(nèi)置方法(BIF)
clear() #刪除字典內(nèi)所有元素
copy() #返回一個字典的淺復(fù)制
fromkeys() #初始化一個新字典
對象
,以序列seq中元素做字典的鍵,val為字典所有鍵對應(yīng)的初始值
c
=
dict
.
fromkeys
(
[
7
,
8
,
9
]
,
"test"
)
print
(
c
)
get(key, default=None) #返回指定鍵的值,如果值不在字典中返回default值
has_key(key) #如果鍵在字典dict里返回true,否則返回false
items() #以列表返回可遍歷的(鍵, 值) 元組數(shù)組
keys() #以列表返回一個字典所有的鍵
setdefault(key, default=None) #和get()類似, 但如果鍵不已經(jīng)存在于字典中,將會添加鍵并將值設(shè)為default
update(dict2) #把字典dict2的鍵/值對更新到dict里
values() #以列表返回字典中的所有值
六、key的特性
- 不允許同一個鍵出現(xiàn)兩次。創(chuàng)建時如果同一個鍵被賦值兩次,后一個值會被記住
- 鍵必須不可變,所以可以用數(shù),字符串或元組充當(dāng),所以用列表就不行
七、利用字典特性實(shí)現(xiàn)通訊錄功能
tong
=
{
"one"
:
1
}
temp
=
print
(
"|---歡迎進(jìn)入通訊錄程序---|\n|---1:查詢聯(lián)系人資料---|\n|---2:插入新的聯(lián)系人---|\n|---3:刪除已有聯(lián)系人---|\n|---4:退出通訊錄程序---|"
)
while
True
:
instr
=
int
(
input
(
"請輸入相關(guān)的指令代碼:"
)
)
if
instr
==
2
:
contact
=
str
(
input
(
"請輸入聯(lián)系人姓名:"
)
)
if
contact
in
tong
:
instr1
=
str
(
input
(
"您輸入的用戶名已經(jīng)存在-->>"
,
a
,
":"
,
tong
[
a
]
,
"\n是否修改用戶資料(Yes/No):"
)
)
if
instr1
==
"Yes"
:
tel
=
int
(
input
(
"請輸入用戶聯(lián)系方式:"
)
)
tong
[
contact
]
=
tel
else
:
tel
=
int
(
input
(
"請輸入用戶聯(lián)系方式:"
)
)
tong
[
contact
]
=
tel
elif
instr
==
1
:
contact
=
str
(
input
(
"請輸入聯(lián)系人姓名:"
)
)
if
contact
in
tong
:
print
(
tong
[
contact
]
)
else
:
print
(
"通訊錄中沒有此人"
)
elif
instr
==
3
:
contact
=
str
(
input
(
"請輸入聯(lián)系人姓名:"
)
)
if
contact
in
tong
:
del
tong
[
contact
]
else
:
print
(
"通訊錄中沒有此人"
)
elif
instr
==
4
:
print
(
"|---感謝使用通訊錄程序---|"
)
break
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
