【发布时间】:2017-12-14 18:51:54
【问题描述】:
我有两个字典列表:
list_1 = [
{'total': 18, 'lead_status': '2'},
{'total': 18, 'lead_status': '9'},
{'total': 18, 'lead_status': '8'},
{'total': 16, 'lead_status': '15'},
{'total': 17, 'lead_status': '14'}
]
list_2 = [
{'total': 18, 'lead_status': '2'},
{'total': 22, 'lead_status': '9'},
{'total': 18, 'lead_status': '8'},
{'total': 16, 'lead_status': '15'},
{'total': 17, 'lead_status': '14'}
]
lead_status 始终具有唯一值,并且列表中字典的顺序可能相同也可能不同。
我想检查每个 lead_status 的 total 值在两个列表中是否相同
示例
对于lead_status : '2',两个列表都有相同的total,即18,然后返回True
对于lead_status : '9',两个列表都有不同的total,在list_1 中是18,但在list_2 中是22。所以它返回False。
我已经尝试过这个解决方案的答案:Comparing 2 lists consisting of dictionaries with unique keys in python
请帮助解决这个问题。任何帮助表示赞赏。
【问题讨论】:
-
我不明白想要的输出是什么,有趣的是会返回一个布尔值列表吗? total:boolean?
-
期望的 o/p 可以是布尔值。因为我正在检查每个
lead_status的total在两个列表中是否相同 -
[list_1[idx]['total'] == list_2[idx]['total'] for idx in range(len(list_1))]为什么这不起作用? -
两个列表是否总是以相同的“lead_status”升序排序?这些列表的大小会一直相同吗?
-
另外,你这样做是为了什么?
标签: python python-3.x list dictionary