原始数据
# 商品信息
dict_commodity_infos = {
1001: {"name": "苹果", "price": 10000},
1002: {"name": "香蕉", "price": 10000},
1003: {"name": "梨", "price": 52100},
1004: {"name": "榴莲", "price": 20},
1005: {"name": "菠萝蜜", "price": 30},
}
# 订单列表
list_orders = [
{"cid": 1001, "count": 1},
{"cid": 1002, "count": 3},
{"cid": 1004, "count": 2},
]
需求:
1. 打印所有商品信息
格式:商品编号xx,商品名称xx,商品单价xx.
for key, value in dict_commodity_infos.items():
print("商品编号%s,商品名称%s,商品单价%s." % (key, value["name"], value["price"]))
"""
输出结果:
商品编号1001,商品名称苹果,商品单价10000.
商品编号1002,商品名称香蕉,商品单价10000.
商品编号1003,商品名称梨,商品单价52100.
商品编号1004,商品名称榴莲,商品单价20.
商品编号1005,商品名称菠萝蜜,商品单价30.
"""
2. 打印所有订单中的信息
格式:商品编号xx,购买数量xx.
for item in list_orders:
print(f"商品编号{item['cid']},购买数量{item['count']}.")
"""
输出结果:
商品编号1001,购买数量1.
商品编号1002,购买数量3.
商品编号1004,购买数量2.
"""
3. 打印所有订单中的商品信息
格式:商品名称xx,商品单价:xx,数量xx.
for item in list_orders:
cid = item["cid"] # 1001
commodity = dict_commodity_infos[cid]
# commodity = dict_commodity_infos[item["cid"]]
# commodity["name"] # 屠龙刀
# commodity["price"] # 10000
print(f"商品名称{commodity['name']},商品单价:{commodity['price']},数量{item['count']}.")
"""
输出结果:
商品名称苹果,商品单价:10000,数量1.
商品名称香蕉,商品单价:10000,数量3.
商品名称榴莲,商品单价:20,数量2.
"""
4. 查找数量最多的订单
格式:商品编号xx,购买数量xx.
【注】(使用自定义算法,不使用内置函数)
for i in range(len(list_orders)-1):
top_order = list_orders[i]
if top_order["count"] < list_orders[i+1]["count"]:
top_order = list_orders[i+1]
print(top_order)
"""
输出结果:
{'cid': 1002, 'count': 3}
"""
5. 根据购买数量对订单列表降序(大->小)排列
格式:商品编号xx,购买数量xx.
for r in range(len(list_orders) - 1):
for c in range(r + 1, len(list_orders)):
if list_orders[r]["count"] < list_orders[c]["count"]:
list_orders[r], list_orders[c] = list_orders[c], list_orders[r]
print(list_orders)
"""
输出结果:
[{'cid': 1002, 'count': 3}, {'cid': 1004, 'count': 2}, {'cid': 1001, 'count': 1}]
"""
6. 增加一条记录
1006: {"name": "猕猴桃", "price": 500}
dict_commodity_infos[1006] = {"name": "猕猴桃", "price": 500}
# dict_commodity_infos[1006] = {("name","猕猴桃"),("price","500")}也可,只是数据显示上位置会发生变化
print(dict_commodity_infos)
"""
输出结果:
{1001: {'name': '苹果', 'price': 10000}, 1002: {'name': '香蕉', 'price': 10000}, 1003: {'name': '梨', 'price': 52100}, 1004: {'name': '榴莲', 'price': 20}, 1005: {'name': '菠萝蜜', 'price': 30}, 1006: {('price', '500'), ('name', '猕猴桃')}}
"""
7. 删除刚刚添加的数据
del dict_commodity_infos[1006]
print(dict_commodity_infos)
"""
输出结果:
{1001: {'name': '苹果', 'price': 10000}, 1002: {'name': '香蕉', 'price': 10000}, 1003: {'name': '梨', 'price': 52100}, 1004: {'name': '榴莲', 'price': 20}, 1005: {'name': '菠萝蜜', 'price': 30}}{1001: {'name': '苹果', 'price': 10000}, 1002: {'name': '香蕉', 'price': 10000}, 1003: {'name': '梨', 'price': 52100}, 1004: {'name': '榴莲', 'price': 20}, 1005: {'name': '菠萝蜜', 'price': 30}, 1006: {('price', '500'), ('name', '猕猴桃')}}
"""
8. 修改id为1002的数据
name改成芭乐,price改成985
dict_commodity_infos[1002]["name"] = "芭乐"
dict_commodity_infos[1002]["price"] = 985
print(dict_commodity_infos)
"""
输出结果:
{1001: {'name': '苹果', 'price': 10000}, 1002: {'name': '芭乐', 'price': 985}, 1003: {'name': '梨', 'price': 52100}, 1004: {'name': '榴莲', 'price': 20}, 1005: {'name': '菠萝蜜', 'price': 30}}
"""