update:dic1.update(dict2) dict2中如果含有dic1中的键则覆盖,否则将新增。
#1
tu = ("alex", [11, 22, {"k1": 'v1', "k2": ["age", "name"], "k3": (11,22,33)}, 44])
#1)元组的特性是不可变,只能查看不能增删改相当于只读列表。
#2)不可以修改
#3)列表,可被修改。
tu[1][2]["k2"].append("Seven")
print(tu)
#4)元组,不可修改
#2
dic = {'k1': "v1", "k2": "v2", "k3": [11,22,33]}
for key in dic.keys():
print(key)
for value in dic.values():
print(value)
for k,v in dic.items():
print(k,v)
dic["k4"] = "v4"
print(dic)
dic["k1"] = "alex"
print(dic)
dic["k3"].append(44)
print(dic)
dic["k3"].insert(0,18)
print(dic)
#3
av_catalog = {
"欧美":{
"www.youporn.com": ["很多免费的,世界最大的","质量一般"],
"www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
"letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
"x-art.com":["质量很高,真的很高","全部收费,屌丝请绕过"]
},
"日韩":{
"tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","verygood"]
},
"大陆":{
"1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
}
}
#a,给此 ["很多免费的,世界最大的","质量一般"]列表第二个位置插入一个 元素:'量很大'。
av_catalog["欧美"]["www.youporn.com"].insert(2,"量很大")
print(av_catalog)
del av_catalog["欧美"]["x-art.com"][1]
print(av_catalog)
av_catalog["日韩"]["tokyo-hot"][1] = av_catalog["日韩"]["tokyo-hot"][1].upper()
print(av_catalog)
av_catalog["大陆"]["1048"] = "一天就封了"
print(av_catalog)
del av_catalog["欧美"]["letmedothistoyou.com"]
print(av_catalog)
av_catalog["大陆"]["1024"].append("可以爬下来")
print(av_catalog)
#4)有字符串"k:1|k1:2|k2:3|k3:4" 处理成字典 {'k':1,'k1':2....}
s = 'k:1|k1:2|k2:3|k3:4'
dic = {}
for i in s.split('|'):
i = i.split(':')
dic.setdefault(i[0], int(i[1]))
print(dic)
#5)将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中
li= [11,22,33,44,55,66,77,88,99,90]
dic = {}
v1 = []
v2 = []
for i in li:
if i > 66:
v1.append(i)
else:
v2.append(i)
dic["k1"] = v1
dic["k2"] = v2
print(dic)
#6)
goods = [{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}, ]
#1:页面显示 序号 + 商品名称 + 商品价格,如: