python如何比较嵌套序列

时间:2016-11-14 06:35:27

标签: python python-2.7 nested comparison tuples

所以我遇到了一些我不明白的事情。给出:

table  # Type: tuple(tuple(str)).
data  # Type: list(list(str)). Both ordered by rows.

当运行以下两次比较时,输出不同,我想知道第一次输出的原因:

table == tuple(tuple(x for x in row) for row in data) -> False
all(table[i] == tuple(data[i]) for i in xrange(len(table))) -> True

==如何处理嵌套序列而不是嵌套序列?

进行比较的数据:

data  = [['A1', 'B1', 'C1', 'D1'], ['A2', 'B2', 'C2', 'D2'], ['A3', 'B3', 'C3', 'D3'], ['A4', 'B4', 'C4', 'D4'], ['1', '2', '3', '4'], ['11', '2', '3', '1'], ['1.1', '2.2', '3.3', '4.0'], ['11', '2', '3', '1'], ['Area', '', 'None', 'Area Error']]
table = (('A1', 'B1', 'C1', 'D1'), ('A2', 'B2', 'C2', 'D2'), ('A3', 'B3', 'C3', 'D3'), ('A4', 'B4', 'C4', 'D4'), ('1', '2', '3', '4'), ('11', '2', '3', '1'), ('1.1', '2.2', '3.3', '4.0'), ('11', '2', '3', '1'), ('Area', '', 'None', 'Area Error'))

抱歉,我在交互式提示中检查后发现了它。 table是名为rows的其他对象的属性。我犯的错误是我过于习惯传递rows索引和迭代器出现(返回rows.table个元素),我忘记它的结构与table不同。实际错误:

rows == tuple(tuple(x for x in row) for row in data)  # Should be `rows.table`.

抱歉一塌糊涂。 然而比较问题仍然存在。

2 个答案:

答案 0 :(得分:1)

您错过了)行中的all。当我添加丢失的paren时,我得到两行都返回true:

data  = [['A1', 'B1', 'C1', 'D1'], ['A2', 'B2', 'C2', 'D2'], ['A3', 'B3', 'C3', 'D3'], ['A4', 'B4', 'C4', 'D4'], ['1', '2', '3', '4'], ['11', '2', '3', '1'], ['1.1', '2.2', '3.3', '4.0'], ['11', '2', '3', '1'], ['Area', '', 'None', 'Area Error']]
table = (('A1', 'B1', 'C1', 'D1'), ('A2', 'B2', 'C2', 'D2'), ('A3', 'B3', 'C3', 'D3'), ('A4', 'B4', 'C4', 'D4'), ('1', '2', '3', '4'), ('11', '2', '3', '1'), ('1.1', '2.2', '3.3', '4.0'), ('11', '2', '3', '1'), ('Area', '', 'None', 'Area Error'))

print table == tuple(tuple(x for x in row) for row in data)  # True
print all(table[i] == tuple(data[i]) for i in xrange(len(table)))  # True

答案 1 :(得分:1)

问题 ==如何处理嵌套序列而不是嵌套序列?

比较即==仅适用于文字,即int,string等。但是,当我们比较lists,list of lists等嵌套数据类型或具有高级别嵌套的内容时,例如list of dictionaries with key as string and values as tuples of integers,比较将在嵌套中出现的(literal, position on literal)对上以第一种方式工作。下面是一个支持我的论点的示例代码:

请注意,每个类都可以根据自己的要求重载==方法。

# The following method compares both nested as well as non-nested data
# and returns True or False.
# for simplicity let us restrict ourself to  basic data-types like
# list,dict,set,tuple,int,float,bool and string.

 basicTypes = [type(1),type(1.0),type("shasha"),type(True)]

def comp(o1,o2):
   if type(o1) == type(o2) and type(o1) not in basicTypes and len(o1) == len(o2):
        if type(o1) == type({}):
            o1 = o1.items()
            o2 = o2.items()

        for a,b in zip(o1,o2):
            if not comp(a,b):
                return False
        return True

    elif type(o1) == type(o2) and type(o1) in basicTypes:
        return o1 == o2

    return False

测试用例:

a= ['a', 1, [2, 3, {1:2, 2:3, 3:[1, 2, 3], 4:{1:2, 2:True }}], {1:[1,2,"sja"],2:{1:2, 2:3}}, (1,2),[(1, 2),{1:2}]]
b= ['a', 1, [2, 3, {1:2, 2:3, 3:[1, 2, 3], 4:{1:2, 2:True }}], {1:[1,2,"sja"],2:{1:2, 2:3}}, (1,2),[(1, 2),{1:2}]]

print(comp(a,b))  #True

a= ['a', 1, [2, 3, {1:2, 2:3, 3:[1, 2, 3], 4:{1:2, 2:True }}], {1:[1,2,"sja"], 5:4}, (1,2),[(1, 2),{1:2}]]
b= ['a', 1, [2, 3, {1:2, 2:3, 3:[1, 2, 3], 4:{1:2, 2:True }}], {1:[1,2,"sja"], 5:2}, (1,2),[(1, 2),{1:2}]]

print(comp(a,b)) #False

a=5
b="shasha"

print(comp(a,b)) #False

a=4
b=5

print(comp(a,b)) #False