深入比较两个元组列表(Python)

2021-05-16 08:29:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python非常陌生,事实上,编程也是如此。 我有两个元组列表,它们之间没有重复(列表是通过列表理解消除重复元组的结果)。 我想在包含的元组(new/deleted/changed)中找到交集,并创建了以下示例:

包含组、项和计数器的元组列表:

list1 = [("fruits","apple",1),("fruits","banana",1),("legumes","bean",3),("meats","deadbeef",1),("meats","pork",2)]

list2 = [("fruits","apple",2),("fruits","mango",4),("legumes","pea",3),("plants","rose",1)]

我的目标是这样的:

*Report*:

New group:     "plants"    with item "rose", count 2

Deleted group: "meats"     with items "deadbeef", count 1, "pork", count 2

New item in group "fruits": "mango", count 4

New item in group "legumes": "pea", count 3

Deleted item in group "fruits": "banana", count 1

Deleted item in group "legumes": "bean", count 3

Changed item in group "fruits": item "apple" new count 2, old count 1

任何帮助都是非常感谢的,因为我完全被困在如何甚至开始:(

我甚至不确定我是否选择了正确的数据结构,所以我对每一个提示都完全开放。你知道吗

谢谢你

乔尔