简体   繁体   English

如何比较python中的两个列表并返回不匹配

[英]How can I compare two lists in python and return not matches

I would like to return values from both lists that not in the other one:我想从两个列表中返回另一个不在另一个列表中的值:

bar = [ 1,2,3,4,5 ]
foo = [ 1,2,3,6 ]

returnNotMatches( a,b )

would return会回来

[[ 4,5 ],[ 6 ]]

One of the simplest and quickest is:最简单和最快的方法之一是:

new_list = list(set(list1).difference(list2))

BONUS!奖金! If you want an intersection (return the matches):如果你想要一个交叉点(返回匹配项):

new_list = list(set(list1).intersection(list2))

Just use a list comprehension:只需使用列表理解:

def returnNotMatches(a, b):
    return [[x for x in a if x not in b], [x for x in b if x not in a]]

This should do这应该做

def returnNotMatches(a, b):
    a = set(a)
    b = set(b)
    return [list(b - a), list(a - b)]

And if you don't care that the result should be a list you could just skip the final casting.如果你不关心结果应该是一个列表,你可以跳过最后的转换。

I might rely on the stdlib here...我可能会在这里依赖 stdlib ......

from itertools import tee, izip
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

import difflib

def returnNotMatches(a, b):
    blocks = difflib.SequenceMatcher(a=a, b=b).get_matching_blocks()
    differences = []
    for b1, b2 in pairwise(blocks):
        d1 = a[b1.a + b1.size: b2.a]
        d2 = b[b1.b + b1.size: b2.b]
        differences.append((d1, d2))
    return differences

print returnNotMatches([ 1,2,3,4,5 ], [ 1,2,3,6 ])

which prints: [([4, 5], [6])]打印: [([4, 5], [6])]

This compares the sequences as streams and finds the differences in the streams.这将序列作为流进行比较并找到流中的差异。 It takes order into account, etc. If order and duplicates don't matter, then sets are by far the way to go (so long as the elements can be hashed).这需要秩序考虑,等等。如果为了和副本并不重要,那sets是目前走(只要元素可以被散列)的方式。

您可以使用列表理解和 zip

''.join([i[0] for i in zip(a, a.lower()) if i[0] == i[1]])

暂无
暂无

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

相关问题 如何比较python中的两个列表并通过电子邮件返回匹配项 - How can I compare two lists in python and return matches by email 如何比较python中的两个列表并返回匹配项 - How can I compare two lists in python and return matches 如果每个字符串中的索引匹配,如何按索引比较两个 python 列表返回 Boolean? - How can I compare two python lists by index return a Boolean if the index in each string matches? python:比较两个列表并按顺序返回匹配项 - python: compare two lists and return matches in order 如何比较两个字符串列表并返回匹配项 - How to compare two lists of strings and return the matches 如何比较两个不同大小的列表,找到匹配项并在两个列表中返回这些匹配项的索引 - How do I compare two lists of diffrent sizes, find matches and return the indices of those matches in both lists 我如何比较 python 中的两个列表,如果我有匹配项 ~> 我想要另一个列表中的匹配项和下一个值 - how can I compare two lists in python and if I have matches ~> I want matches and next value from another list 我如何比较python中的两个列表,并返回第二个需要具有相同值而不管顺序? - How can I compare two lists in python, and return that the second need to have the same values regardless of order? 如何比较两个不同对象的列表并返回匹配项? - How Can i compare two lists of different objects and return match? 在Python中,如何比较两个列表并获取匹配的所有索引? - In Python, how to compare two lists and get all indices of matches?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM