字典,作业
Python学习日记-day15
字典:python中唯一一种具有映射关系的数据类型,字典查询效率要高于列表,字典存储空间比列表大,用空间换取时间
key:value
键:值 对
key 必须唯一 且为不可变数据类型
字典是无序的
# 创建字典 ,如果字典里有重复的 如两个 name , 不会报错,但查询的时候会只取到最后一个
names = {
1234562333: {"name": "alex", "age": "25"},
2223562333: {"name": "jack", "age": "25"},
3334562333: {"name": "jimu", "age": "25"},
444: ["cuihua"]
}
# 打印字典的元素
print(names)
# 修改444的第1个元素为xiaowang:
names[444][0] = "xiaowang"
print(names)
# 向444里追加一个元素wangbadan
names[444].append("wangbadan")
print(names)
# 增加新的555
names[555] = "mike"
print(names)
# 删除
names.pop(1111) # 有1111这个key则返回这个key对应的值,表示删除成功,没有则触发error
names.pop(1111, None) # 没有1111这个key会触发error,不触发可以指定None给他进行返回
names.popitem() # 随机删除一个
del names[112]
# 查询
print(names) # 查所有
print(names[112]) # 查指定的,没有会报错
print(names.get(112)) # 不想出错可以这样写,如果不存在返回None 推荐
# 快速判断key在不在字典里
print(112 in names) # 有返回Ture,没有返回False
print(names.keys()) # 列出所有的key
print(names.values()) # 列出所有的value
# Loop 循环
for i in names:
print(i) # 只会打印key
print(i, names[i]) # 同时打印key和value
# 不要用这种方式,效率低
for k, v in names.items():
print(k.v) # 同时打印key和value
for i in names.items():
print(i) # 也会同时打印key和value,但是有括号括起来
# 2个字典合并
dict1 = {
110: {"name": "alex"},
112: {"name": "jack"},
}
dict2 = {
110: {"name": "alex chen"},
118: {"name": "newuser"},
}
dict1.update(dict2) # 将dict2合并到dict1 , 如有相同的key,则覆盖,不存在的增加进去
作业1:使用字典优化购物车程序
# 需求:
# 1. 程序开始提示输入工资,打印商品列表
# 2. 输入商品列表序号加入购物车,显示余额,如果余额不够进行提示
# 3. 选择好后退出打印购物车列表,显示花费金额以及余额
# 4. 结算退出。
product_list = [["Iphone X", 8000], ["MacBook", 12000], ["路由器", 119], ["999感冒灵", 10], ["纸巾", 10], ["玩具汽车", 48], ["茶杯", 49]]
cart = []
cart_list = {}
salary = input("请输入你的工资:")
salary = salary.strip()
if salary.isdigit():
salary = int(salary)
while True:
print("我们有以下商品供你选择:")
index = 0
for i in product_list:
print(index, i)
index += 1
choice = input("请输入商品编号进行购买,退出请按q:")
choice = choice.strip()
if choice.isdigit():
choice = int(choice)
if choice >= 0 and choice < len(product_list):
product = product_list[choice]
if product[1] <= salary:
if product[0] in cart_list:
cart_list[product[0]][1] += 1
else:
cart_list[product[0]] = [product[1], 1]
salary -= product[1]
print("添加到--> " + product[0] + " <--购物车成功! 你还有余额" + str(salary) + "元")
else:
print("大哥你买不起,还差" + str(product[1] - salary) + "元")
elif choice == "q":
id_count = 1
total = 0
print("----------------您的购物清单----------------")
print("id\t\t商品\t\t数量\t\t单价\t\t总价")
for i in cart_list:
print("%s %s %s %s %s" % (
id_count, i, cart_list[i][1], cart_list[i][0], cart_list[i][1] * cart_list[i][0]))
id_count += 1
total += (cart_list[i][1] * cart_list[i][0])
print("您的总消费为:" + str(total) + "元")
print("您的余额为:" + str(salary) + "元")
print("----------------end-----------------")
break
else:
print("超出选择范围了!")
else:
print("输入无效!")
作业2:三级菜单,b返回上级,q退出
# 需求:三级菜单,b返回上级,q退出
provice = {
"四川省": {
"成都市": {
"金牛区": {
"茶店子": {},
"交大路": {}
},
"青羊区": {
"光华村": {},
"杜甫草堂": {}
},
"锦江区": {
"春熙路": {}
},
"成华区": {
"驷马桥": {}
},
"武侯区": {
"高升桥": {},
"红牌楼": {}
},
"高新区": {
"天府软件园": {},
"腾讯": {},
"IBM": {}
}
},
"宜宾市": {
"翠屏区": {},
"长宁县": {},
"南溪区": {},
"江安县": {}
}
},
"北京市": {
"东城区": {
},
"朝阳区": {
}
}
}
while True:
for i in provice:
print(i)
choice = input("please input L1:")
if choice == "q":
exit()
if choice in provice:
while True:
for j in provice[choice]:
print(j)
choice2 = input("please input L2:")
if choice2 == "b":
break
if choice2 == "q":
exit()
if choice2 in provice[choice]:
while True:
for k in provice[choice][choice2]:
print(k)
choice3 = input("please input L3:")
if choice3 == "b":
break
if choice3 == "q":
exit()
if choice3 in provice[choice][choice2]:
while True:
for m in provice[choice][choice2][choice3]:
print(m)
ch4 = input("b or q:")
if ch4 == "b":
break
if ch4 == "q":
exit()

共有 0 条评论