Common methods for Python dictionaries

Source: Internet
Author: User

#Author is Lizhenlei


#字典的特性:
# dict is disordered.
# key must be unique, so naturally go heavy


#key: Value (key: Values)
info = {
' stu1101 ': "Tenglan Wu",
' stu1102 ': "Longze Luola",
' stu1103 ': "Xiaoze Maliya",
}
Print (info)
Print (info[' stu1101 ') #查找字典
info["stu1101"] = "Enrique" #修改字典内容
info["stu1104"] = "Cangjingkong" #存在即修改, there is no increase

#del info["stu1101"]
Info.popitem () #随便删除任意一个字典中的键值对

Print (Info.get (' stu1104 ')) #不出错查找字典内容

Print (' stu1103 ' in info) #查找字典中是否存在 info.has_key (' 1103 ') in Python 2.x

#多级嵌套字典及操作 dictionaries can be inline lists or inline dictionaries

Print (info)

Info.values () #打印所有不包括key的值
Info.keys () #打印所有key值



Av_catalog = {
"Europe and America": {
"Www.youporn.com": ["a lot of free, the world's largest", "General quality"],
"Www.pornhub.com": ["a lot of free, also very big", "quality than Yourporn high"],
"Letmedothistoyou.com": ["mostly selfie, high-quality pictures many", "resources are not many, update slow"],
"X-art.com": ["high quality, really high", "all charges, Dick than please bypass"]
},
"Japan and Korea": {
"Tokyo-hot": ["How the quality is not clear, the individual has not liked the Japanese and Korean fan", "Heard is charged"]
},
"Continent": {
"1024": ["All free, really good, good Life Peace", "server in foreign countries, slow"]
}
}

av_catalog["mainland" ["1024"][1] = ' can mirror in China '

Av_catalog.setdefault ("Continent", {' www.baidu.com ': [Up]}) #在字典中寻找key值, can be taken unchanged, not taken, and assigned a new value

Print (Av_catalog)


Info2 = {
' stu1101 ': "Tenglan Wu",
' stu1102 ': "Longze Luola",
' stu1103 ': "Xiaoze Maliya",
}

b = {
' stu1101 ': "Alex",
1:3,
2:5
}

Info2.update (b)
Info2.items ()
Print (Info2.items ()) #把一个字典转成列表

c = Dict.fromkeys ([6,7,8],[1,{"name": "Alex"},444]) #初始化一个新的字典
C[7][1] = "Jeck Chen"
#c [6][0] = 9
Print (c)


Info3 = {
' stu1101 ': "Tenglan Wu",
' stu1102 ': "Longze Luola",
' stu1103 ': "Xiaoze Maliya",
}

For i in Info3: #字典的循环 more efficient
Print (I, info3[i])

For k,v in Info3.items (): #字典的循环
Print (k, v)

Common methods for Python dictionaries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.