1.tu = ("alex", [11, 22, {"k1": 'v1', "k2": ["age", "name"], "k3": (11, 22, 33)}, 44])
a.元组特性:不可改变列表,只读.
b.alex不可修改.
c.k2对应的值为列表,可以修改.
tu[1][2]["k2"].append("Seven")
print(tu)
d.元组,不可修改.
2.dic = {'k1': "v1", "k2": "v2", "k3": [11, 22, 33]}
a.print(dic.keys())
b.print(dic.values())
c.for k,v in dic.items():
print(k,v)
d.dic.setdefault("k4","v4")
print(dic)
e.dic['k1'] = 'alex'
print(dic)
f.dic['k3'].append(44)
print(dic)
g.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(1,'量很大')
b.av_catalog['欧美']['x-art.com'].pop()
c.av_catalog['欧美']['x-art.com'].append("金老板最喜欢这个")
d.av_catalog['日韩']["tokyo-hot"][1] = av_catalog['日韩']["tokyo-hot"][1].upper()
e.av_catalog['大陆']["1048"] = ["一天就封了"]
或者av_catalog['大陆'].setdefault('1048',["一天就封了"])
f.del av_catalog["欧美"]["letmedothistoyou.com"]
或者av_catalog["欧美"].pop("letmedothistoyou.com")
g.av_catalog["大陆"]["1024"][0] = av_catalog["大陆"]["1024"][0] + "可以爬下来"
4.有字符串 "k:1|k1:2|k2:3|k3:4" 处理成字典 {'k':1,'k1':2....}
s = "k:1|k1:2|k2:3|k3:4"
lst = s.split("|")
dic = {}
for el in lst:
k,v = el.split(":")
dic[k] = int(v)
print(dic)
5.元素分类
li= [11,22,33,44,55,66,77,88,99,90],将大于 66 的值保存至字典的第一个key中,小于 66 的值保存至第二个key的值中。即: {'k1': 大于66的所有值列表, 'k2': 小于66的所有值列表}
li= [11,22,33,44,55,66,77,88,99,90]
dic = {'k1':[],"k2":[]}
for a in li:
if a > 66:
dic['k1'].append(a)
elif a < 66:
dic["k2"].append(a)
else:
pass
print(dic)
或
li= [11,22,33,44,55,66,77,88,99,90]
dic = {}
for a in li:
if a > 66:
dic.setdefault('k1',[]).append(a)
elif a < 66:
dic.setdefault('k2',[]).append(a)
else:
pass
print(dic)
6.输出商品列表,用户输入序号,显示用户选中的商品
商品列表:
goods = [{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}, ]
要求:
1:页面显示序号 + 商品名称 + 商品价格,如:
1 电脑 1999
2 鼠标 10
...
2:用户输入选择的商品序号,然后打印商品名称及商品价格.
3:如果用户输入的商品序号有误,则提示输入有误,并重新输入。
4:用户输入Q或者q,退出程序。
goods = [{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}, ]
for a in range(len(goods)):
good = goods[a]
print(a+1,good["name"],good["price"])
while 1:
b = input("请输入序号:")
if b.upper() == "Q":
break
c = int(b)-1
if c in range(4):
print(goods[c]["name"], goods[c]["price"])
break
else:
print("输入有误,重新输入:")