【发布时间】:2018-12-05 19:05:58
【问题描述】:
我有一个list = [0, 0, 7],当我使用in 将它与anotherList = [0, 0, 7, 0] 进行比较时,它给了我False。
我想知道如何检查一个列表中的数字是否与另一个列表的顺序相同。
所以,如果我这样做anotherList2 = [7, 0, 0, 0]:
list in anotherList2 返回 False
但是,list in anotherList 返回 True
【问题讨论】:
-
你不应该列出
list,它会覆盖一个内置名称。此外,list in anotherList将在此处返回 False,而不是 True -
在这种情况下,
in检查列表是否为anotherList的元素,而不是list的元素是否包含在anotherList中。如果你先anotherList.append(list)然后list in anotherList,你会看到它返回True -
在更大的数据集上构造一个滑动窗口迭代器,看看是否有任何窗口等于您的第一个列表:stackoverflow.com/questions/6822725/…
标签: python list comparison comparison-operators