Python 将听写中的所有列表相互比较

Python 将听写中的所有列表相互比较,python,python-object,Python,Python Object,在python中,是否可以比较以下结构中的所有对象 我有一个列表字典,每个列表中都有对象,例如 [ [object1,object2,object3], [object4,object5,object6], [object7,object8,object9], ] 我希望通过每个列表中的属性将所有对象相互比较,并确定哪些对象不在每个列表中 根据反馈,请参见下面的示例 希望这有帮助,我想这就是你想要的。我们创建一组obj.no的可能值,然后使用set-diff

在python中,是否可以比较以下结构中的所有对象

我有一个列表字典,每个列表中都有对象,例如

[
      [object1,object2,object3],
      [object4,object5,object6],
      [object7,object8,object9],
]
我希望通过每个列表中的属性将所有对象相互比较,并确定哪些对象不在每个列表中

根据反馈,请参见下面的示例


希望这有帮助,我想这就是你想要的。我们创建一组obj.no的可能值,然后使用set-difference操作符在两个集合上使用-来获取缺少的元素

# Get a set of all the no. values present in the data.
combined_set_of_values = set([item.no for item in data])

# Get the sets of obj.no values grouped by description.
for obj in data:
    groups[obj.description].append(obj.no)

new_list = groups.values()


# Print the list, and the elements missing from that list
for list in new_list:
    print("Values in list:")
    print(list)
    # Use set difference to see what's missing from list.
    print("Missing from list:")
    print(combined_set_of_values - set(list))
这将提供以下输出:

Values in list:
[1, 2]
Missing from list:
{3, 4}
Values in list:
[1, 2, 3]
Missing from list:
{4}
Values in list:
[1, 2, 4]
Missing from list:
{3}

这似乎是一个列表列表,而不是一个列表字典。@jpp我添加了一个示例,希望如此helps@AndrewMcDowell您是对的,这是一个列表。您想知道每个列表中缺少哪些对象,或者缺少哪些self.no数字吗?@AndrewMcDowell每个列表中缺少哪些self.no数字,t虽然对象工作得太理想了,但我只需要一个列表,列出列表中缺少的东西,谢谢你,非常感谢你,这正是我想要的:没问题。感谢您编辑您的问题,使其更清楚!
Values in list:
[1, 2]
Missing from list:
{3, 4}
Values in list:
[1, 2, 3]
Missing from list:
{4}
Values in list:
[1, 2, 4]
Missing from list:
{3}