Python 017 study notes

dictionary

{Key: value, key: value}

No location relationship, only mappings.

ID (a) a variable memory address

 

Immutable type: integer, string, a tuple

Variable types: lists, dictionaries

 

Key: Immutable

Value: Variable

Two main characteristics: disorderly, Unique

If duplicate keys exist, a replacement before the last

 

Create

dic={'name':'Kevin','age':39,'job':'teacher','marriage':True}
print(dic)

d=dict((range(2),(range(10,12))))
print(d)
dic1=dict((('sex','male'),))
print(dic1)

dic2=dict.fromkeys(['host1','host2','host3'],'baby')
print(dic2)

dic3=dict.fromkeys(['host1','host2','host3'],['baby','candy'])
print(dic3)

dic4=dict.fromkeys(dic,dic1)
print(dic4)

check

= {DIC 'name': 'Kevin', 'Age': 39, 'Job': 'Teacher', 'Marriage': True} 
Print (DIC)

# find the value of a bond
Print (DIC [ 'name'] )

# list all key names in the dictionary
Print (dic.keys ())

# is converted to the list
Print ( list (dic.keys ()))

# list all the values in the dictionary
Print (dic.values ())

# convert list
Print ( list (dic.values ()))

# listed in the dictionary all keys
Print (dic.items ())

# is converted into a list, composed of a tuple
Print ( list (dic.items ()))

increase

dic={'name':'Kevin','age':39,'job':'teacher','marriage':True}
print(dic)

#字典增加单个元素,没有键值就增加,已有键值就修改
dic['name']='xy'
print(dic)

dic['sex']='male'
print(dic)

# .setdefault 如有已有键值,不做改变,否者增加键值,有返回值,返回字典值对应键的值
ret1=dic.setdefault('name','xia')
print(ret1)
print(dic)

ret2=dic.setdefault('class','2')
print(ret2)
print(dic)

#合并两个字典,不同键值合并,相同键,更新值
dic_a={'name':'kevin','age':'38'}
dic_b={'hobby':'music','sex':'male'}
dic_a.update(dic_b)
print(dic_a)

dic_c={'name':'kevin','age':'38','sex':'female'}
dic_d={'hobby':'music','sex':'male'}
print(dic_c)
dic_c.update(dic_d)
print(dic_c)

#字典嵌套
av_catalog = {
"欧美":{
"www.youporn.com": ["很多免费的,世界最大的","质量一般"],
"www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
"letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
"x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
},
"日韩":{
"tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
},
"大陆":{
"1024":["全部免费,真好,好人一生平安","服务器在国外,"]
}
}

av_catalog["欧美"]["www.youporn.com"][1]="高清无码"
print(av_catalog)

dic={'name':'Kevin','age':39,'job':'teacher','marriage':True}
print(dic)

#通过键名删除键值
del dic['name']
print(dic)

ret=dic.pop('age')
print(ret)
print(dic)

# 随机删除某个键值,并以元组形式返回值
dic1={'name':'Kevin','age':39,'job':'teacher','marriage':True}
print(dic1)

ret1=dic1.popitem()
print(ret1)
print(dic1)

#清空整个字典
dic1.clear()
print(dic1)

#删除整个字典
del dic1
print(dic1)

Guess you like

Origin www.cnblogs.com/wtzxxy/p/12418309.html