【问题标题】:How python compares nested sequencespython如何比较嵌套序列
【发布时间】:2016-11-14 06:35:27
【问题描述】:

所以我陷入了我不明白的事情。给定:

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`.

对不起,一团糟。 但是比较问题仍然存在。

【问题讨论】:

  • 能否给表格和数据的样本数据
  • 向我们展示我们可以实际运行的代码,它实际演示了运行时的问题。您发布的all 版本应该给出TypeError,特别是TypeError: 'bool' object is not iterable
  • 我怀疑data 实际上比table 长。 table 甚至可能是空的。
  • 现在你已经修正了错字,我得到True 用于 Python 2.6 上的两个测试,datatable。顺便说一句,tuple(row) 返回与tuple(x for x in row) 相同的内容,只是它更短且更高效。
  • @PM2Ring 你说得对,谢谢。

标签: python python-2.7 nested comparison tuples


【解决方案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

【讨论】:

  • comp() 实现很好地解释了它。我最近在 Mark Lutz 的“Learning Python”中读到过,据说序列/映射比较会一一比较它们的项目(如果不是可散列的)。但是我不知道它是否以 BFS 或 DFS 方式发生。
【解决方案2】:

您在all 行中缺少)。当我添加缺少的括号时,我得到两行都返回 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

【讨论】:

    猜你喜欢
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    相关资源
    最近更新 更多