字典
1.查询内存地址
a = 10
print(id(a))
b = a
print(id(b))
b = 15
print(id(b))
2. 数据类型
不可变类型:整型、字符串、元组
可变类型:列表、字典
字典的值可以是任意类型,键只能是不可变类型。
字典存储是无序的。例如:
dic = {'name':'ysw','age':36,'hobby':'game','is_handsome':True}
print(dic)
print(dic['name'])
#dic = {1:'ysw','age':36,'hobby':'game','is_handsome':True}
#print(dic)
#print(dic[1]) #键改为整型没有问题
#dic = {[1,2]:'ysw','age':36,'hobby':'game','is_handsome':True}
#print(dic)
#print(dic[[1,2]]) #键改为列表出现问题
#dic = {'name':'ysw','age':36,'hobby':{'game_name':'wow','level':'20'},'is_handsome':True}
#print(dic['hobby'])
#dic = {{'1':'123'},'age':36,'hobby':{'game_name':'wow','level':'20'},'is_handsome':True}
#print(dic)
#字典两大特点:无序;键唯一
#dic = {'age':'ysw','age':36,'hobby':'game','is_handsome':True}
#print(dic)
3.字典的两种创建方式:
#dic1 = {'name':'ysw'}
#dic2 = dict((('name','ysw'),))
4.对应操作
(1)增
dic1 = {'name':'ysw'}
dic1['age'] = 18
print(dic1)
dic1['name'] = 18
print(dic1)
#没有重复的键就添加,有重复的键就替换数值
------------------------------
dic1 = {'name':'ysw'}
dic1['age'] = 18
print(dic1)
dic1.setdefault('age',36)
print(dic1)
#dic1.setdefault('hobby','game')
#print(dic1)
#setdefault的特点:没有重复的键就添加,并返回添加键的值;有重复的键就没有变化,并返回字典中重复键的值。
#ret = dic1.setdefault('age',36)
#print(ret)
#ret2 = dic1.setdefault('hobby','game')
#print(dic1)
#print(ret2)
(2)查
利用键名来查找键的值
dic3 = {'name':'ysw','age':36,'hobby':'game','is_handsome':True}
print(dic3['name'])
print(dic3.keys()) #查找字典中所有的键名,结果并不是列表类型,可以通过list命令转换为列表
print(list(dic3.keys()))
print(dic3.values()) #查看字典中所有键对应的值,结果并不是列表类型,可以通过list命令转换为列表
print(list(dic3.values()))
print(dic3.items()) #查看字典中所有键值对 ,结果并不是列表类型,可以通过list命令转换为列表
print(list(dic3.items()))
(3)改
dic1 = {'name':'ysw'}
dic1['name'] = 18
print(dic1)
-------------------------------------------------------
dic4 = {'name':'ysw','age':36,'hobby':'game','is_handsome':True}
dic5 = {'1':'111','2':'222','age':'35'}
dic4.update(dic5) #dic5与dic4没有重复的将dic5添加到dic4,有重复的就更新键值对;dic5保持不变。
print(dic4)
print(dic5)
#删:
# dic = {'name':'ysw','age':36,'hobby':'game','is_handsome':True}
# del dic['name'] #del 删除指定键值对
# print(dic)
#dic = {'name':'ysw','age':36,'hobby':'game','is_handsome':True}
# dic.clear() #clear清空字典
# print(dic)
# ret = dic.pop('age') #pop删除指定键值对,并返回删除的键值对的值
# print(ret)
# print(dic)
# a = dic.popitem() #popitem随机删除一组键值对,并以元组方式返回值
# print(a,dic)
# del dic #删除整个字典,内存中不再有该字典
# print(dic)
#其他操作以及涉及到的方法:
# dic6 = dict.fromkeys(['host1','host2','host3'],'test')
# print(dic6)
#字典嵌套
# av_catalog = {
# "欧美":{
# "www.youporn.com": ["很多免费的,世界最大的","质量一般"],
# "www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
# "letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
# "x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
# },
# "日韩":{
# "tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
# },
# "大陆":{
# "1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
# }
# }
# #
# # av_catalog["大陆"]["1024"][1] += ",可以用爬虫爬下来"
# # print(av_catalog["大陆"]["1024"])
# # #ouput
# # ['全部免费,真好,好人一生平安', '服务器在国外,慢,可以用爬虫爬下来']
# av_catalog['欧美']['www.youporn.com'][1] = '高清无码'
# print(av_catalog)
#字典排序 根据键的值进行排列
# dic = {5:'555',2:'666',4:'444'}
# print(sorted(dic))
# print(sorted(dic.values()))
# print(sorted(dic.items()))
#字典的遍历
# dic5 = {'name':'ysw','age':36}
# for i in dic5:
# print(i)
# print(i,dic5[i]) #推荐这一种方法,效率高
# for i in dic5.items():
# print(i)
# for i,v in dic5.items():
# print(i,v)