文章目录
-
-
- 1.根据需求写代码
- 2.根据需求写代码
- 3.循环提示用户输入,并将输入内容添加到字典中(如果输入N或n则停止运行)
- 4.判断以下值那个能做字典的key ?那个能做集合的元素?
- 5.将字典的键和值分别追加到key_list和value_list 两个列表中,如:
- 6.字典dic = {"k1":"v1","k2":"v2","k3": [11,22,33])
- 7.请循环打印k2对应的值中的每个元素。
- 8.有字符串"k: 1|k1:2| k2:3| k3 :4处理成字典{'k':1,'k1':2..….}
- 9.写代码
- 10.输出商品列表,用户输入序号,显示用户选中的商品
1.根据需求写代码
dic = { "k1": "v1","k2": "v2" , "k3": [ 11,22,33]}
dic.update({"k4":"v4"})
dic["k4"] = v4
print(dic)
dic.update({"k1":"Ken"})
dic["k1"] = Ken
print(dic)
dic["k3"].appdend(44)
print(dic)
dic["k3"].insert(0,18)
print(dic)
2.根据需求写代码
dic = {
'name' : ["bob",2,3,5],
'job': "teacher",
'subject':{'english':["xingainian","cc",100]}
}
dic['name'].append("qq")
print(dic)
new_name = dic["name"][0].capitalize()
dic["name"][0] = new_name
print(dic)
new_name1 = dic["name"][0].upper()
dic["name"][0] = new_name1
print(dic)
dic['subject']["dd"]="gg"
print(dic)
dic['subject']["english"].pop(1)
print(dic)
3.循环提示用户输入,并将输入内容添加到字典中(如果输入N或n则停止运行)
dic = dict()
while True:
user_input = input("请输入内容:(例如:用户输入x1 | aa;输入N/n退出)")
li = ""
if user_input.upper() == "N":
print("程序结束")
break
else:
li = user_input.split("|")
dic.setdefault(li[0],li[1])
print(dic)
4.判断以下值那个能做字典的key ?那个能做集合的元素?
- -1:都可以
- n:都可以
- None:都可以
- [1,2]:可以做集合的元素
- (1,):都可以
- {11,22,33,44}:可以做集合的元素
- {‘name’:‘john’,‘age’:18}:可以做集合的元素
5.将字典的键和值分别追加到key_list和value_list 两个列表中,如:
key_list =[]
value_list =[]
info = { "k1" : "v1 " ,"k2 " : "v2 " ,"k3" : "v3"}
for keys,values in info.items():
key_list.append(keys)
value_list.append(values)
print(key_list)
print(value_list)
key_list =list(info.keys())
value_list =list(info.values())
print(key_list)
print(value_list)
6.字典dic = {“k1”:“v1”,“k2”:“v2”,“k3”: [11,22,33])
dic = {"k1":"v1","k2":"v2","k3": [11,22,33]}
for keys in dic:
print(keys)
for keys,values in dic.items():
print(keys)
for keys,values in dic.items():
print(values)
for keys,values in dic.items():
print(keys,values)
7.请循环打印k2对应的值中的每个元素。
info = {
'k1 ' : 'vl',
'k2': [ ("qiqi"), ("miaomiao"),("doudou")]
}
for keys,values in info.items():
if keys == "k2":
for item in values:
print(item)
8.有字符串"k: 1|k1:2| k2:3| k3 :4处理成字典{‘k’:1,‘k1’:2…….}
str1 = "k:1|k1:2|k2:3|k3:4"
dic = {}
l1 = str1.split("|")
for item in l1:
l2 = item.split(":")
dic.setdefault(l2[0],l2[1])
print(dic)
9.写代码
"""
有如下值 li = [11,22,33,44,55,66,77,88,99,90],将所有大于66的值保存至字典的第一个key对应的列表中,将小于66的值保存至第二个key对应的列表中。
result ={"k1":[],"k2":[]》
"""
li = [11,22,33,44,55,66,77,88,99,90]
result = {}
n1 = []
n2 = []
for num in li:
if num > 66:
n1.append(num)
if num < 66:
n2.append(num)
result.setdefault("k1",n1)
result.setdefault("k2",n2)
print(result)
10.输出商品列表,用户输入序号,显示用户选中的商品
"""
商品列表:
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}
]
i = 0
for item in goods:
i=i+1
print(i,item.get("name"),item.get("price"))
while True:
user_input = input("请输入商品序号:(输入Q或者q,退出程序)")
if user_input.upper() == "Q":
break
if int(user_input.isdecimal()) :
if int(user_input) <= len(goods):
result = goods[int(user_input)-1].get("price")
print(result)
else:
print("您的输入有误。")
else:
print("您的输入有误。")