简体   繁体   English

Python比较2个列表,其中填充了3个元组

[英]Python compare 2 lists filled with 3-tuples

I have 2 lists each filled with the 3-tuples. 我有2个列表,每个列表都填充了3个元组。

In the beginning they will be equal: 一开始它们将相等:

a = [(1,1,123),(1,2,124),(2,1,231),(2,2,123)]
b = [(1,1,123),(1,2,124),(2,1,231),(2,2,123)]

Case 1: 情况1:

An extra element is added to b at the end 最后将一个额外的元素添加到b

a = [(1,1,123),(1,2,124),(2,1,231),(2,2,123)]
b = [(1,1,123),(1,2,124),(2,1,231),(2,2,123),(3,1,123)]

Return: Added (3,1,123)

Case 2 情况二

Element 2 in b changes from 124 -> 123 b元素2从124 > 123变为

a = [(1,1,123),(1,2,124),(2,1,231),(2,2,123)]
b = [(1,1,123),(1,2,123),(2,1,231),(2,2,123)]

Return: Changed a[1] to (1,2,123)

Case 3 情况3

A combination of Case 1 and 2, an extra element is added to b and element 2 in b changes from 124 -> 123 案例1和2的组合,一个额外的元素被添加到b和元件2在b的变化从124 - > 123

a = [(1,1,123),(1,2,124),(2,1,231),(2,2,123)]
b = [(1,1,123),(1,2,123),(2,1,231),(2,2,123),(3,1,123)]

Return: Added (3,1,123) and Changed a[1] to (1,2,123)

In all the cases a==b returns False, what I'm trying to do is figure out how to compare the lists and find whether an element was added, changed, or both. 在所有情况下, a==b返回False,我想做的就是弄清楚如何比较列表并查找是否添加,更改或同时添加了元素。 Looking for any suggestions. 寻找任何建议。

changed = False
added = len(tuple1) != len(tuple2)
for e1, e2 in zip(tuple1, tuple2):
    if e1 != e2:
        changed = True
return changed, added, a==b

This is before the edit. 这是在编辑之前。 This will check to see if added, changed, or the same and return three booleans accordingly. 这将检查是否添加,更改或相同,并相应地返回三个布尔值。

tuple1 = [(1,1,123),(1,2,124),(2,1,231),(2,2,123)]
tuple2 = [(1,1,123),(1,2,124),(2,1,231),(2,2,13), (0,0)]
changed = False
info = ""
if len(tuple1) != len(tuple2):
    info += "Added " + str(tuple2[-1])
for e1, e2 in zip(tuple1, tuple2):
    if e1 != e2:
        info += " Changed a[" + str(tuple2.index(e2)) + "] to " + str(e2)
print(info)

prints ---> "Added (0, 0) Changed a[3] to (2, 2, 13)" 打印--->“添加(0,0)将a [3]更改为(2,2,13)”

The above code is post edit and will work in any instance where only one element is added and the element is added to the second to tuple or "b". 上面的代码是后期编辑的,并且可以在仅添加一个元素并将该元素添加到第二个元组或“ b”的任何情况下使用。

added = len(b) > len(a)
changed = b[:len(a)] != a
both = added and changed

You could compare if len(b) > len(b) to check if an element was added or not, and if False , then compare if a==b or not. 您可以比较len(b) > len(b)来检查是否添加了元素,如果False ,则比较a==b或否。

if len(b) > len(a):
    if b[:len(a)] == a:
        print 'Added only'
    else:
        print 'Added and changed'
else:
    if b != a:
        print 'Changed'
    else:
        print 'No changes'

List is a mutable sequence , when comparing lists, it won't actually compare it's contents. List是一个可变序列 ,比较列表时,它实际上不会比较其内容。

However, tuple, is immutable. 但是,元组是不可变的。 comparing tuples will give you expected behavior in your case. 比较元组会给您预期的行为。

tuple(a) == tuple(b)

It's a simple way to test whether a == b. 这是测试a == b的简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM