#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__:anxu.qi
# Date:2018/11/30
info = {
`stu1101`: "TengLan Wu",
`stu1102`: "LongZe Luola",
"stu1103": "XiaoZe Maliya",
}
####################################### 多級字典的巢狀 #############################################
# key 儘量不要寫中文,因為有時候編碼不一致,取不出來值
av_catalog = {
"歐美":{
"www.youporn.com": ["很多免費的,世界最大的","質量一般"],
"www.pornhub.com": ["很多免費的,也很大","質量比yourporn高點"],
"letmedothistoyou.com": ["多是自拍,高質量圖片很多","資源不多,更新慢"],
"x-art.com":["質量很高,真的很高","全部收費,屌比請繞過"]
},
"日韓":{
"tokyo-hot":["質量怎樣不清楚,個人已經不喜歡日韓範了","聽說是收費的"]
},
"大陸":{
"1024":["全部免費,真好,好人一生平安","伺服器在國外,慢"]
}
}
av_catalog["大陸"]["1024"][1] = "可以再國內做映象"
print(av_catalog)
# 列印所有的值 values()
print(av_catalog.values())
# 列印所有的key keys()
print(av_catalog.keys())
# 字典是無序的,是沒有下標的
#################### 字典 查 #################################
print(info)
# {`stu1103`: `XiaoZe Maliya`, `stu1102`: `LongZe Luola`, `stu1101`: `TengLan Wu`}
# 方式一:
print(info["stu1101"]) # 存在則不會報錯,不存在就會報錯
# 方式二: .get
print(info.get("stu110113")) # 有就返回該值,沒有這個值就返回None
# None
# TengLan Wu
# 方式三: in
print("stu1103" in info) # 等與 python2.x info.has_key("stu1103")
#################### 字典 改 #################################
info["stu1101"] = "武藤蘭" # 存在直接替換
print(info)
# {`stu1102`: `LongZe Luola`, `stu1101`: `武藤蘭`, `stu1103`: `XiaoZe Maliya`}
#################### 合併更新 update ####################################
a = {
`stu01`:"nginx",
`stu02`:"mysql",
`stu03`:`zabbix`
}
b = {
`stu01`:"tami",
1:3,
2:5
}
a.update(b)
print(a)
# {`stu03`: `zabbix`, `stu02`: `mysql`, 2: 5, `stu01`: `tami`, 1: 3}
# 把b作為一個引數傳為了update,把兩個字典合併,如果a字典中有和b字典中相同的key,則覆蓋a中的value
# 如果沒有這個kye那麼,將b中的key和value都新增到a字典中
print(b)
#################### 字典 增 ##################################
info["stu1104"] = "CangJingkong" # 不存在則建立一條
print(info)
# {`stu1104`: `CangJingkong`, `stu1102`: `LongZe Luola`, `stu1101`: `武藤蘭`, `stu1103`: `XiaoZe Maliya`}
# 建立一個新的值 ,去字典裡能取到我就返回,如果取不到我就設定一個新的。
av_catalog.setdefault("TaiWan", {"www.baiddu.com":[1, 2]})
print(av_catalog)
#################### 字典 刪 ##################################
# 方式一:
del info["stu1101"]
print(info)
# {`stu1104`: `CangJingkong`, `stu1102`: `LongZe Luola`, `stu1103`: `XiaoZe Maliya`}
# 方式二:
info.pop("stu1102")
print(info)
# {`stu1103`: `XiaoZe Maliya`, `stu1104`: `CangJingkong`}
# 方式三: 隨機刪除
info.popitem()
print(info)
# {`stu1103`: `XiaoZe Maliya`}
c = {
`stu01`: "tami",
1: 3,
2: 5
}
# ##################### fromkeys #####################
# 初始化一個字典,並附一個臨時的值,和淺copy 類似,只copy一層。
d = dict.fromkeys([6,7,8])
print(d)
# {8: None, 6: None, 7: None}
d = dict.fromkeys([6,7,8],"test")
print(d)
# {8: `test`, 6: `test`, 7: `test`}
d = dict.fromkeys([6,7,8],[1,{"name":"alex"},444])
print(d)
# {8: [1, {`name`: `alex`}, 444], 6: [1, {`name`: `alex`}, 444], 7: [1, {`name`: `alex`}, 444]}
d[7][1][`name`] = "Jack wang"
print(d)
# {8: [1, {`name`: `Jack wang`}, 444], 6: [1, {`name`: `Jack wang`}, 444], 7: [1, {`name`: `Jack wang`}, 444]}
##################### 迴圈 #################
a = {
`stu01`:"nginx",
`stu02`:"mysql",
`stu03`:`zabbix`
}
for i in a: # 迴圈字典的時候預設是迴圈字典的key
print(i)
# stu01
# stu03
# stu02
print( a.items())
# dict_items([(`stu03`, `zabbix`), (`stu02`, `mysql`), (`stu01`, `nginx`)])
######################################
# 迴圈字典的key 和value
# 注:以下方式一和方式二的區別:結果都是一樣的,但是方式一比方式二高效很多,
# 因為方式一隻是通過key,找到key和value,方式二是將字典轉換為列表後,通過key,value找到對應的值
# 資料量不大還可以,資料量大的情況下非常佔用資源費時間。
# 方式一:
for i in a:
print(i,a[i])
# stu03 zabbix
# stu01 nginx
# stu02 mysql
# 方式二:
for k,v in a.items():
print(k,v)
# stu03 zabbix
# stu01 nginx
# stu02 mysql
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__:anxu.qi
# Date:2018/11/21
############################ 字典 ##################################
# 字典的每一個元素,都是鍵值對, 而且是無序的
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
## 索引
# print(user_info[`age`])
# 30
## 切片沒有
######################## for 迴圈 ########################
# 迴圈預設輸出的是key
for i in user_info:
print(i)
"""
gender
name
age
"""
print("---------------------------------------")
######################## keys #######################
# 加上keys後,會輸出所有的key
for i in user_info.keys():
print(i)
"""
gender
name
age
"""
print("---------------------------------------")
######################## values #######################
# 加上values後,會輸出所有的value
for i in user_info.values():
print(i)
"""
Vmiss
Vsidaodeng
V30
"""
print("---------------------------------------")
######################## items #######################
# 加上items 後,會輸出所有的item
for k,v in user_info.items():
print(k,v)
"""
Kname Vsidaodeng
Kgender Vmiss
Kage V30
"""
##########################################################
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
# 獲取字典的所有的key
print(user_info.keys())
# dict_keys([`age`, `gender`, `name`])
# 獲取字典的所有的值
print(user_info.values())
# dict_values([30, `M`, `sidaodeng`])
# 獲取字典所有的鍵值對
print(user_info.items())
# dict_items([(`age`, 30), (`name`, `sidaodeng`), (`gender`, `M`)])
################################ del 刪除 ##################################################
# 刪除字典中的某個鍵值對
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
del user_info[`Kage`]
print(user_info,11110000000)
# {`Kname`: `Vsidaodeng`, `Kgender`: `Vmiss`} 11110000000
############################### clear ##########################################
# def clear(self): # real signature unknown; restored from __doc__
# 清楚所有內容
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
user_info.clear()
print(user_info)
# {}
############################### copy ##########################################
# def copy(self): # real signature unknown; restored from __doc__
# 淺copy
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
# 先忽略,後期補充
############################### fromkeys ##########################################
# def fromkeys(S, v=None): # real signature unknown; restored from __doc__
#
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
# 先忽略,後期補充
############################### get ##########################################
# def get(self, k, d=None): # real signature unknown; restored from __doc__
# 根據key獲取值,如果key不存在,可以指定一個預設值。d是預設值
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
aa = user_info.get(`Kname`) # 如果有這個值,就會列印出來value值
print(aa)
# Vsidaodeng
aa = user_info.get(`Knameadasfad`) # 如果沒有這個kye,就會列印None,但是不會報錯
print(aa)
# None
# get 新增預設值
aa = user_info.get(`Kage`,`123`) # 如果存在這個key,就會將對應的value列印出來
print(aa,1111)
# V30 1111
aa = user_info.get(`Kage111`,`123`) # 如果沒有aeg111這個key,那麼就會給一個預設值123
print(aa)
# 123
######### 通過索引也可以取,如果沒有這個key,就會報錯 ########
######### 所以,推薦使用,get("key") 來獲取。################
print(user_info[`Kname`])
# Vsidaodeng
# print(user_info[`Kname111111`])
# Kname
# # print(user_info[`Kname111111`])
# # Kage
# # KeyError: `Kname111111`
############################### in ##########################################
# # 是檢查字典中否有key
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
aaaa = `Kname` in user_info.keys()
print(aaaa)
# True
aaaa = `Kname1231` in user_info.keys()
print(aaaa)
# False
############################### items ##########################################
# def items(self): # real signature unknown; restored from __doc__
# 所有項的列表形式
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
############################### iteritems ##########################################
# def iteritems(self): # real signature unknown; restored from __doc__
# 項可迭代
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
# 先不說,後期補充
############################### viewvalues ##########################################
# def iterkeys(self): # real signature unknown; restored from __doc__
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
############################### viewvalues ##########################################
# def itervalues(self): # real signature unknown; restored from __doc__
# value可迭代 """
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
############################### keys ##########################################
# def keys(self): # real signature unknown; restored from __doc__
# 所有的key列表
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
# 列出所有的key
############################### pop ##########################################
# def pop(self, k, d=None): # real signature unknown; restored from __doc__
# 獲取某個key,並在字典中移除掉
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
bb = user_info.pop(`Kage`)
print(bb)
# V30
print(user_info)
# {`Kgender`: `Vmiss`, `Kname`: `Vsidaodeng`}
#
############################### popitem ##########################################
# def popitem(self): # real signature unknown; restored from __doc__
# 獲取並在字典中移除,隨機移除。
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
bbbb = user_info.popitem()
print(bbbb)
"""
(`Kage`, `V30`)
(`Kgender`, `Vmiss`)
(`Kname`, `Vsidaodeng`)
"""
############################### setdefault ##########################################
# def setdefault(self, k, d=None): # real signature unknown; restored from __doc__
# 如果key不存在,則建立,如果存在,則返回已存在的值且不修改 """
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
############################### update ##########################################
# def update(self, E=None, **F): # known special case of dict.update
# 批量更新
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
print(user_info)
# {`Kgender`: `Vmiss`, `Kage`: `V30`, `Kname`: `Vsidaodeng`}
hehe = {
"a":1,
"b":2,
"c":3
}
user_info.update(hehe)
print(user_info)
# {`b`: 2, `Kgender`: `Vmiss`, `Kage`: `V30`, `a`: 1, `c`: 3, `Kname`: `Vsidaodeng`}
########################### update 新增 #############################################
hehe = {
"a":1,
"b":2,
"c":3
}
# 方式一:
hehe.update({"d":4})
print(hehe)
# {`b`: 2, `c`: 3, `d`: 4, `a`: 1}
# 方式二:
hehe["e"]=5
# print(hehe)
# {`b`: 2, `c`: 3, `d`: 4, `a`: 1}
舉例說明:
ttt = (11,22,["alex",{"k1":"v1"}])
ttt[2][1]["k2"] = "v2" # 方式一等同於下面的方式二
ttt[2][1].update({"k2":"v2"}) # 方式二
print(ttt)
# (11, 22, [`alex`, {`k2`: `v2`, `k1`: `v1`}])
############################### viewvalues ##########################################
# def values(self): # real signature unknown; restored from __doc__
# """ 所有的值 """
# """ D.values() -> list of D`s values """
# return []
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
############################### viewvalues ##########################################
# def viewitems(self): # real signature unknown; restored from __doc__
# """ 所有項,只是將內容儲存至view物件中 """
# """ D.viewitems() -> a set-like object providing a view on D`s items """
# pass
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
############################### viewvalues ##########################################
# def viewkeys(self): # real signature unknown; restored from __doc__
# """ D.viewkeys() -> a set-like object providing a view on D`s keys """
# pass
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}
############################### viewvalues ##########################################
# def viewvalues(self):
user_info = {
"Kname":"Vsidaodeng",
"Kage":"V30",
"Kgender":"Vmiss"
}