如何比较任何索引中存在的两个字典值列表

时间:2017-06-21 22:42:02

标签: python python-2.7

我有两个词典列表

dict1 = [{'Weight': '134.0', 'date': '2017-06-15 17:44:29', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '14.0', 'date': '2017-06-15 16:21:09', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '154.0', 'date': '2017-06-15 16:21:09', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '184.0', 'date': '2017-06-15 15:47:49', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '104.0', 'date': '2017-06-15 17:11:09', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '134.0', 'date': '2017-06-15 17:27:49', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}]
dict2 = [{'Weight': '124.0', 'date': '2017-06-15 17:35:19', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '184.0', 'date': '2017-06-15 15:47:49', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '114.0', 'date': '2017-06-15 17:11:09', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '14.0', 'date': '2017-06-15 16:21:09', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}]

结果应该在一个字典列表中匹配,非匹配应该在其他字典中

示例

matched:   [{'Weight': '14.0', 'date': '2017-06-15 16:21:09', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}]
nonmatched: [{'Weight': '124.0', 'date': '2017-06-15 17:35:19', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '184.0', 'date': '2017-06-15 15:47:49', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}, {'Weight': '114.0', 'date': '2017-06-15 17:11:09', 'model': '2011', 'serialnumber': 'SN_WEIGHT082', 'unit': 'Kg', 'manufacturer': 'CONTINUA'}]

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。
- matched = [ x for x in dict1 if x in dict2 ]
- unmatched = [ x for x in dict1 if x not in dict2 ]