1. 程式人生 > >Python3基礎_字典

Python3基礎_字典

dropak

# -*- coding:-utf-8 -*-

# Auther:dropak

# 字典操作


# 定義字典

test = {

"sh1":"Mayun",

"sh2":"Xiaobin",

"sh3":"HBp",

}


# 增加

test["sh4"] ="Lmin"

print(test)


# 刪除

test.pop("sh2") #法1

print(test)

del test["sh1"] #法2

print(test)

test.popitem() #隨機刪除

print(test)


# 修改

test["sh1"] ="LiuQD"

print(test)


# 查找

print(test["sh1"])


# 多級字典嵌套操作

av_catalog = {

"歐美":{

"www.youporn.com": ["很多免費的,世界最大的","質量一般"],

"www.pornhub.com": ["很多免費的,也很大","質量比yourporn高點"],

"letmedothistoyou.com": ["多是自拍,高質量圖片很多","資源不多,更新慢"],

"x-art.com":["質量很高,真的很高","全部收費,屌比請繞過"]

},

"日韓":{

"tokyo-hot":["質量怎樣不清楚,個人已經不喜歡日韓範了","聽說是收費的"]

},

"大陸":{

"1024":["全部免費,真好,好人一生平安","服務器在國外,慢"]

}

}


# 修改value值

av_catalog["歐美"]["www.youporn.com"][1] ="質量還行,"

print(av_catalog["歐美"]["www.youporn.com"])


# 循環dict

for n in test: #1,推薦使用

print(n,test[n])

for k,v in test.items(): #2

print(k,v)


本文出自 “運維” 博客,請務必保留此出處http://dropak.blog.51cto.com/12206256/1971478

Python3基礎_字典