python - 1. 打印飞利浦发送的所有货物详情?2.打印所有处于“在途”状态的货物
问题描述
这是给定的字典
dict1= {101: {'Sender': 'Phillip', 'Receiver': 'Ramya', 'Start date': '14-03-2020', 'Delivery date': '25-03-2020', 'Sender location': 'Area 1', 'Receiver location': 'Area 6', 'Delivery status': 'Delivered', 'Shipping cost': 198}, 102: {'Sender': 'Romesh', 'Receiver': 'Phillip', 'Start date': '18-06-2020', 'Delivery date': '09-07-2020', 'Sender location': 'Area 2', 'Receiver location': 'Area 4', 'Delivery status': 'Delivered', 'Shipping cost': 275}, 103: {'Sender': 'Omega lll', 'Receiver': 'Ramya', 'Start date': '01-12-2020', 'Delivery date': 'Null', 'Sender location': 'Area 5', 'Receiver location': 'Area 1', 'Delivery status': 'In Transit', 'Shipping cost': 200}, 104: {'Sender': 'Phillip', 'Receiver': 'John', 'Start date': '23-06-2020', 'Delivery date': '25-06-2020', 'Sender location': 'Area 1', 'Receiver location': 'Area 4', 'Delivery status': 'Delivered', 'Shipping cost': 314}, 105: {'Sender': 'Ramya', 'Receiver': 'Romesh', 'Start date': '29-08-2020', 'Delivery date': '10-09-2020', 'Sender location': 'Area 5', 'Receiver location': 'Area 3', 'Delivery status': 'Delivered', 'Shipping cost': 275}, 106: {'Sender': 'John', 'Receiver': 'Omega lll', 'Start date': '28-06-2020', 'Delivery date': 'Null', 'Sender location': 'Area 3', 'Receiver location': 'Area 1', 'Delivery status': 'In Transit', 'Shipping cost': 270}}
有人可以帮我解决这个问题吗
解决方案
让我们只选择 Sender 是 Phillip 的字典值。
# dict1.values() gives an array of the values of dict1
# filter takes a function that determines which values to keep (lambda) and the dict1.values() array
# in this case it only accepts values with 'Sender' == 'Phillip'
# list converts filter's output into a normal list
dict1 = {101: {'Sender': 'Phillip', 'Receiver': 'Ramya', 'Start date': '14-03-2020', 'Delivery date': '25-03-2020', 'Sender location': 'Area 1', 'Receiver location': 'Area 6', 'Delivery status': 'Delivered', 'Shipping cost': 198}, 102: {'Sender': 'Romesh', 'Receiver': 'Phillip', 'Start date': '18-06-2020', 'Delivery date': '09-07-2020', 'Sender location': 'Area 2', 'Receiver location': 'Area 4', 'Delivery status': 'Delivered', 'Shipping cost': 275}, 103: {'Sender': 'Omega lll', 'Receiver': 'Ramya', 'Start date': '01-12-2020', 'Delivery date': 'Null', 'Sender location': 'Area 5', 'Receiver location': 'Area 1', 'Delivery status': 'In Transit', 'Shipping cost': 200}, 104: {'Sender': 'Phillip', 'Receiver': 'John', 'Start date': '23-06-2020', 'Delivery date': '25-06-2020', 'Sender location': 'Area 1', 'Receiver location': 'Area 4', 'Delivery status': 'Delivered', 'Shipping cost': 314}, 105: {'Sender': 'Ramya', 'Receiver': 'Romesh', 'Start date': '29-08-2020', 'Delivery date': '10-09-2020', 'Sender location': 'Area 5', 'Receiver location': 'Area 3', 'Delivery status': 'Delivered', 'Shipping cost': 275}, 106: {'Sender': 'John', 'Receiver': 'Omega lll', 'Start date': '28-06-2020', 'Delivery date': 'Null', 'Sender location': 'Area 3', 'Receiver location': 'Area 1', 'Delivery status': 'In Transit', 'Shipping cost': 270}}
phillips = list(filter(lambda value: value['Sender'] == 'Phillip',
dict1.values()))
print(phillips)
结果是
[{'Sender': 'Phillip', 'Receiver': 'Ramya', 'Start date': '14-03-2020', 'Delivery date': '25-03-2020', 'Sender location': 'Area 1', 'Receiver location': 'Area 6', 'Delivery status': 'Delivered', 'Shipping cost': 198}, {'Sender': 'Phillip', 'Receiver': 'John', 'Start date': '23-06-2020', 'Delivery date': '25-06-2020', 'Sender location': 'Area 1', 'Receiver location': 'Area 4', 'Delivery status': 'Delivered', 'Shipping cost': 314}]
您可以使用完全相同的策略filter(lambda x: _____, dict1.values())
来查找在途的物品。
推荐阅读
- google-cloud-firestore - 从本地脚本将用户属性发送到谷歌分析
- shell - 注销或返回 Xamarin 表单时如何更新 Shell 页面的内容
- windows - 命令在本地工作,但不能通过 wmic
- javascript - MongoDB/Mongoose:根据数组大小从数组或整个文档中删除一个对象
- android - 在颤振中使用 path_provider 包时出错
- jmeter - 在 JMeter 中每个线程循环运行一次预处理器
- wordpress - windows下怎么安装wordpress?
- javascript - 无法使用 Visual Studio 代码将字符串转换为数组
- r - 数据时间普遍格式的格式
- arrays - 如何在仍然使用异步管道的一个步骤中对一组值的可观察对象进行分区?