# 1. 定义三个集合保存选三⻔课(python、java、h5)学生的名单,求: from itertools import product python1={"张三","李四","王五","赵六","孙七"} java1={"张三","李四","孙七","周八","郑十","吴九"} h5={"王五","吴九","杨三","高七","马一","周八","张三"} # 1)求选了课的所有学生名单 s1=python1|java1|h5 # 2)求只选了python的学生有哪些 s2=python1-java1-h5 # 3)求同时选了三⻔课的学生有哪些 s3=python1&java1&h5 # 4)求只选了一⻔课的学生有哪些 s4=(python1^java1^h5)-s3 # 5)求同时选了两⻔课的学生有哪些 # python2=list(python1) # java2=list(java1) # h52=list(h5) # names=python2+java2+h52 # print([x for x in names if names.count(x) == 2]) s5= s1 - s3 - s4 # 2. 已经一个订单列表里面保存的是20个订单对应的字典 Orders = [{'order_id': 'DD202503310001', 'customer': {'name': '余平', 'phone': '18201796009', 'address': '贵州省杭州市华龙海口街f座 818990'}, 'product_detail': {'name': '戴尔XPS笔记本', 'sku': 'SKU5391', 'quantity': 2, 'unit_price': 18898.97, 'total_price': 37797.94}, 'payment': {'method': '信用卡', 'transaction_id': '2D34A8DC-7991'}, 'logistics': {'status': '已完成', 'estimated_delivery': '2025-04-30'}, 'timestamp': '2025-03-31 00:00:00'}, {'order_id': 'DD202503020002', 'customer': {'name': '周利', 'phone': '15394662335', 'address': '香港特别行政区琴县牧野台北路w座 687849'}, 'product_detail': {'name': '小米扫地机器人', 'sku': 'SKU1446', 'quantity': 1, 'unit_price': 2708.09, 'total_price': 2708.09}, 'payment': {'method': '微信支付', 'transaction_id': '3BC72F75-C990'}, 'logistics': {'status': '已完成', 'estimated_delivery': '2025-03-15'}, 'timestamp': '2025-03-02 00:00:00'}, {'order_id': 'DD202503270003', 'customer': {'name': '王东', 'phone': '13783121777', 'address': '吉林省深圳县崇文淮安街N座 777844'}, 'product_detail': {'name': '小米扫地机器人', 'sku': 'SKU3156', 'quantity': 1, 'unit_price': 2545.86, 'total_price': 2545.86}, 'payment': {'method': '微信支付', 'transaction_id': 'CD2F5BC7-2713'}, 'logistics': {'status': '已发货', 'estimated_delivery': '2025-03-30'}, 'timestamp': '2025-03-27 00:00:00'}, {'order_id': 'DD202503240004', 'customer': {'name': '黄玉梅', 'phone': '15027540837', 'address': '澳门特别行政区汕尾县长寿黎街W座 161451'}, 'product_detail': {'name': '小米扫地机器人', 'sku': 'SKU2339', 'quantity': 1, 'unit_price': 2627.93, 'total_price': 2627.93}, 'payment': {'method': '信用卡', 'transaction_id': '37F9488D-3117'}, 'logistics': {'status': '已取消', 'estimated_delivery': None}, 'timestamp': '2025-03-24 00:00:00'}, {'order_id': 'DD202503180005', 'customer': {'name': '林伟', 'phone': '13086382933', 'address': '山西省佛山县萧山关岭街L座 804003'}, 'product_detail': {'name': 'iPhone 15', 'sku': 'SKU3903', 'quantity': 1, 'unit_price': 8695.43, 'total_price': 8695.43}, 'payment': {'method': '微信支付', 'transaction_id': '7384559F-CE23'}, 'logistics': {'status': '已取消', 'estimated_delivery': None}, 'timestamp': '2025-03-18 00:00:00'}, {'order_id': 'DD202503230006', 'customer': {'name': '祝雪梅', 'phone': '18540644364', 'address': '陕西省旭市上街李路e座 997378'}, 'product_detail': {'name': '大疆无人机', 'sku': 'SKU5722', 'quantity': 3, 'unit_price': 10587.93, 'total_price': 31763.79}, 'payment': {'method': '银联', 'transaction_id': '70E10A22-6260'}, 'logistics': {'status': '已完成', 'estimated_delivery': '2025-04-07'}, 'timestamp': '2025-03-23 00:00:00'}, {'order_id': 'DD202503090007', 'customer': {'name': '王桂芝', 'phone': '18912281431', 'address': '江苏省重庆市海陵拉萨街w座 421341'}, 'product_detail': {'name': '大疆无人机', 'sku': 'SKU5549', 'quantity': 3, 'unit_price': 9062.65, 'total_price': 27187.95}, 'payment': {'method': '支付宝', 'transaction_id': '9C19E082-73BD'}, 'logistics': {'status': '待付款', 'estimated_delivery': '2025-03-09'}, 'timestamp': '2025-03-09 00:00:00'}, {'order_id': 'DD202503240008', 'customer': {'name': '邢霞', 'phone': '18857351706', 'address': '陕西省柳县双滦重庆路G座 371319'}, 'product_detail': {'name': '戴尔XPS笔记本', 'sku': 'SKU9859', 'quantity': 3, 'unit_price': 18317.17, 'total_price': 54951.51}, 'payment': {'method': '银联', 'transaction_id': '9F46C92A-6314'}, 'logistics': {'status': '已取消', 'estimated_delivery': None}, 'timestamp': '2025-03-24 00:00:00'}, {'order_id': 'DD202503050009', 'customer': {'name': '陈伟', 'phone': '15334148984', 'address': '四川省丹丹县清城成都街v座 503469'}, 'product_detail': {'name': '大疆无人机', 'sku': 'SKU6455', 'quantity': 3, 'unit_price': 12124.41, 'total_price': 36373.23}, 'payment': {'method': '信用卡', 'transaction_id': '5B41B561-BBF7'}, 'logistics': {'status': '已发货', 'estimated_delivery': '2025-03-07'}, 'timestamp': '2025-03-05 00:00:00'}, {'order_id': 'DD202503080010', 'customer': {'name': '马楠', 'phone': '15117215979', 'address': '宁夏回族自治区梅县高坪兴城街S座 164739'}, 'product_detail': {'name': '索尼PS5', 'sku': 'SKU8644', 'quantity': 2, 'unit_price': 4256.97, 'total_price': 8513.94}, 'payment': {'method': '信用卡', 'transaction_id': '0805D9B1-8B3A'}, 'logistics': {'status': '待付款', 'estimated_delivery': '2025-03-09'}, 'timestamp': '2025-03-08 00:00:00'}, {'order_id': 'DD202503080011', 'customer': {'name': '张萍', 'phone': '15897417984', 'address': '海南省亮市门头沟王街G座 975428'}, 'product_detail': {'name': '华为Mate60', 'sku': 'SKU4515', 'quantity': 3, 'unit_price': 6863.98, 'total_price': 20591.94}, 'payment': {'method': '支付宝', 'transaction_id': 'BA45BF3D-7E96'}, 'logistics': {'status': '待付款', 'estimated_delivery': '2025-03-09'}, 'timestamp': '2025-03-08 00:00:00'}, {'order_id': 'DD202503190012', 'customer': {'name': '郭建国', 'phone': '18283703520', 'address': '海南省文市江北西宁街A座 472174'}, 'product_detail': {'name': '戴尔XPS笔记本', 'sku': 'SKU7247', 'quantity': 1, 'unit_price': 13125.13, 'total_price': 13125.13}, 'payment': {'method': '信用卡', 'transaction_id': '8E745FA1-F094'}, 'logistics': {'status': '待付款', 'estimated_delivery': '2025-03-20'}, 'timestamp': '2025-03-19 00:00:00'}, {'order_id': 'DD202503170013', 'customer': {'name': '郑娜', 'phone': '18687853832', 'address': '澳门特别行政区深圳县清城银川路R座 146220'}, 'product_detail': {'name': '华为Mate60', 'sku': 'SKU5915', 'quantity': 2, 'unit_price': 7793.62, 'total_price': 15587.24}, 'payment': {'method': '微信支付', 'transaction_id': 'A908E39C-226A'}, 'logistics': {'status': '已取消', 'estimated_delivery': None}, 'timestamp': '2025-03-17 00:00:00'}, {'order_id': 'DD202503060014', 'customer': {'name': '陈婷婷', 'phone': '15742454024', 'address': '江苏省龙县长寿谢街V座 354146'}, 'product_detail': {'name': '戴尔XPS笔记本', 'sku': 'SKU7110', 'quantity': 3, 'unit_price': 18490.92, 'total_price': 55472.76}, 'payment': {'method': '信用卡', 'transaction_id': 'B6D2BF37-5E24'}, 'logistics': {'status': '已取消', 'estimated_delivery': None}, 'timestamp': '2025-03-06 00:00:00'}, {'order_id': 'DD202503110015', 'customer': {'name': '吕兰英', 'phone': '13610733277', 'address': '江苏省莉市房山郭街E座 155399'}, 'product_detail': {'name': '小米扫地机器人', 'sku': 'SKU8779', 'quantity': 1, 'unit_price': 2884.77, 'total_price': 2884.77}, 'payment': {'method': '银联', 'transaction_id': '349E37E3-E9FE'}, 'logistics': {'status': '已取消', 'estimated_delivery': None}, 'timestamp': '2025-03-11 00:00:00'}, {'order_id': 'DD202503200016', 'customer': {'name': '杨艳', 'phone': '13939986043', 'address': '新疆维吾尔自治区建华县山亭沈阳街h座 916796'}, 'product_detail': {'name': '戴尔XPS笔记本', 'sku': 'SKU9313', 'quantity': 1, 'unit_price': 18516.56, 'total_price': 18516.56}, 'payment': {'method': '信用卡', 'transaction_id': '8CD09A30-6786'}, 'logistics': {'status': '待付款', 'estimated_delivery': '2025-03-20'}, 'timestamp': '2025-03-20 00:00:00'}, {'order_id': 'DD202503020017', 'customer': {'name': '廖云', 'phone': '13055778106', 'address': '海南省合肥县海陵柳州街K座 401916'}, 'product_detail': {'name': '大疆无人机', 'sku': 'SKU8577', 'quantity': 1, 'unit_price': 8570.74, 'total_price': 8570.74}, 'payment': {'method': '支付宝', 'transaction_id': '9904FD28-A095'}, 'logistics': {'status': '待付款', 'estimated_delivery': '2025-03-03'}, 'timestamp': '2025-03-02 00:00:00'}, {'order_id': 'DD202503290018', 'customer': {'name': '谢桂珍', 'phone': '18503748330', 'address': '陕西省萍市和平合肥路Q座 554169'}, 'product_detail': {'name': '小米扫地机器人', 'sku': 'SKU1595', 'quantity': 3, 'unit_price': 2757.85, 'total_price': 8273.55}, 'payment': {'method': '银联', 'transaction_id': '6FB95553-039B'}, 'logistics': {'status': '待付款', 'estimated_delivery': '2025-03-29'}, 'timestamp': '2025-03-29 00:00:00'}, {'order_id': 'DD202503290019', 'customer': {'name': '朱畅', 'phone': '13004398008', 'address': '浙江省俊市南溪兴城街C座 931599'}, 'product_detail': {'name': '大疆无人机', 'sku': 'SKU7838', 'quantity': 3, 'unit_price': 7466.06, 'total_price': 22398.18}, 'payment': {'method': '银联', 'transaction_id': 'CD303682-CFEB'}, 'logistics': {'status': '已取消', 'estimated_delivery': None}, 'timestamp': '2025-03-29 00:00:00'}, {'order_id': 'DD202503020020', 'customer': {'name': '安桂珍', 'phone': '15973299476', 'address': '江西省哈尔滨县普陀李街t座 446022'}, 'product_detail': {'name': '大疆无人机', 'sku': 'SKU9573', 'quantity': 2, 'unit_price': 5297.74, 'total_price': 10595.48}, 'payment': {'method': '信用卡', 'transaction_id': '620B1575-C9E2'}, 'logistics': {'status': '已完成', 'estimated_delivery': '2025-03-26'}, 'timestamp': '2025-03-02 00:00:00'}] # 1)求待付款的总的定单金额 jine=0 for x in Orders: daifukuan=x["logistics"]["status"] daifujine=x["product_detail"]["total_price"] if daifukuan=="待付款": jine +=daifujine print(round(jine,2)) # 2)分别统计不同支付方式对应的订单数量 dict1={} for x in Orders: zhifu = x["payment"]["method"] dict1[zhifu] = dict1.get(zhifu,0) +1 print(dict1) # 3)统计已取消的总订单数 count1=0 for x in Orders: if x["logistics"]["status"] == "已取消": count1 += 1 print(count1) # 4)分别统计三月前15天和后15天的总的订单金额 dict1={} from datetime import datetime q1=datetime(2025,3,1,0,0,0) q2=datetime(2025,3,16,0,0,0) q3=datetime(2025,3,17,0,0,0) q4=datetime(2025,4,1,0,0,0) for x in Orders: q5=datetime.strptime(x["timestamp"],"%Y-%m-%d %H:%M:%S") daifujine = x["product_detail"]["total_price"] if q1<q5<q2 : dict1["上半月"]=dict1.get("上半月",0) + daifujine elif q3<q5<q4 : dict1["下半月"] = dict1.get("下半月", 0) + daifujine print(dict1) print("===================分割线===================") dict2={} for x in Orders: q5=datetime.strptime(x["timestamp"],"%Y-%m-%d %H:%M:%S") daifujine = x["product_detail"]["total_price"] if q5.day <= 15 : dict2["上半月"] = dict2.get("上半月", 0) + daifujine else: dict2["下半月"] = dict2.get("下半月", 0) + daifujine print(dict2) # 5)找出产生订单金额最多的是哪一天 dict2={} for x in Orders: daifujine = x["product_detail"]["total_price"] fukuanriqi = x["timestamp"] #if x["logistics"]["status"] != "已取消": dict2[fukuanriqi] = dict2.get(fukuanriqi,0) + daifujine max1=1 day1='' for x in dict2: if dict2[x] > max1: max1= dict2[x] day1= x print(day1)
11.homework
本节14078字2025-04-07 15:08:42