python 比较两个列表并打印出差异的 Pythonic 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2387981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:32:12  来源:igfitidea点击:

Pythonic way to compare two lists and print out the differences

pythoncomparison

提问by randomThought

I have two lists which are guaranteed to be the same length. I want to compare the corresponding values in the list (except the first item) and print out the ones which dont match. The way I am doing it is like this

我有两个保证长度相同的列表。我想比较列表中的相应值(第一项除外)并打印出不匹配的值。我这样做的方式是这样的

i = len(list1)
if i == 1:
    print 'Nothing to compare'
else:
    for i in range(i):
        if not (i == 0):
            if list1[i] != list2[i]:
                print list1[i]
                print list2[i]

Is there a better way to do this? (Python 2.x)

有一个更好的方法吗?(Python 2.x)

回答by Mizipzor

list1=[1,2,3,4]
list2=[1,5,3,4]
print [(i,j) for i,j in zip(list1,list2) if i!=j]

Output:

输出:

[(2, 5)]

Edit:Easily extended to skip nfirst items (same output):

编辑:轻松扩展以跳过第n个项目(相同的输出):

list1=[1,2,3,4]
list2=[2,5,3,4]
print [(i,j) for i,j in zip(list1,list2)[1:] if i!=j]

回答by RyanWilcox

Nobody's mentioned filter:

没有人提到过滤器:

a = [1, 2, 3]
b = [42, 3, 4]

aToCompare = a[1:]
bToCompare = b[1:]

c = filter( lambda x: (not(x in aToCompare)), bToCompare)
print c

回答by Jim

You could use sets:

你可以使用集合:

>>> list1=[1,2,3,4]
>>> list2=[1,5,3,4]
>>> set(list1[1:]).symmetric_difference(list2[1:])
set([2, 5])

回答by Jimmy

edit: oops, didn't see the "ignore first item" part

编辑:哎呀,没有看到“忽略第一项”部分

from itertools import islice,izip

for a,b in islice(izip(list1,list2),1,None):
    if a != b:
       print a, b

回答by Eli Bendersky

There's a nice class called difflib.SequenceMatcherin the standard library for that.

difflib.SequenceMatcher为此,标准库中有一个很好的类。

回答by Phil H

Noting the requirement to skip the first line:

注意跳过第一行的要求:

from itertools import izip
both = izip(list1,list2)
both.next()  #skip the first
for p in (p for p in both if p[0]!=p[1]):
   print pair
  1. This uses izip, an iterator (itertools) version of zip, to generate an iterator through pairs of values. This way you don't use up a load of memory generating a complete zipped list.
  2. It steps the bothiterator by one to avoid processing the first item, and to avoid having to make the index comparison on every step through the loop. It also makes it cleaner to read.
  3. Finally it steps through each tuple returned from a generator which yields only unequal pairs.
  1. 这使用izip的迭代器 ( itertools) 版本zip,通过值对生成迭代器。这样您就不会消耗大量内存来生成完整的压缩列表。
  2. 它将both迭代器逐个步进,以避免处理第一项,并避免在循环的每一步都必须进行索引比较。它还使阅读更干净。
  3. 最后,它遍历从生成器返回的每个元组,生成器只产生不相等的对。

回答by shivam bhatnagar

You can use set operationto find Symmetric difference (^) between two lists. This is one of the best pythonic ways to do this.

您可以使用set operation来查找两个列表之间的对称差异 (^)。这是执行此操作的最佳 Pythonic 方法之一。

list1=[1,2,3,4]
list2=[1,5,3,4]

print(set(list1) ^ set(list2))

So the output will be {2, 5}

所以输出将是 {2, 5}