找到具有相同值的列表中的差异

时间:2016-09-25 13:33:33

标签: python

除了两个添加的元素之外,我需要能够找到列表中可能具有相同值的差异

例如

a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y']

问题如何,'a task''another task'都跟着'j'

[x for x in b if x not in a]
['a task']

由于ab都包含'j',因此会将其从列表中删除。

我怎么做才能最终得到

['a task', 'j']

4 个答案:

答案 0 :(得分:1)

对于简单列表 - 您要求的只是搜索列表中的下一个项目:

>>> a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
>>> b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y']
>>> c = [[x, b[b.index(x) + 1]] for x in b if x not in a]
>>> c
[['a task', 'j']]

但我认为你实际上是在使用字典或元组。

<强>元组:

>>> a = [('cool task', 'b'), ('another task', 'j'), ('better task', 'y')]
>>> b = [('cool task', 'b'), ('a task', 'j'), ('another task', 'j'), ('better task', 'y')]
>>> c = [x for x in b if x not in a]
>>> c
[('a task', 'j')]

<强>字典:

>>> a = {'cool task': 'b', 'another task': 'j', 'better task': 'y'}
>>> b = {'cool task': 'b', 'a task': 'j', 'another task': 'j', 'better task': 'y'}
>>> c = [(x, b[x]) for x in b if x not in a]
>>> c
[('a task', 'j')]

答案 1 :(得分:1)

您可以使用difflib.SequenceMatcher() class枚举添加,删除和更改的条目:

>>> from difflib import SequenceMatcher
>>> matcher = SequenceMatcher(a=a, b=b)
>>> added = []
>>> for tag, i1, i2, j1, j2 in matcher.get_opcodes():
...     if tag == 'insert':
...         added += b[j1:j2]
...
>>> added
['a task', 'j']

以上仅关注增加的条目;如果您需要了解删除或更改的条目,那么这些事件也有操作码,请参阅SequenceMatcher.get_opcodes() method documentation

但是,如果您的条目始终是配对,那么只需生成带有元组的集合(使用pair-wise iteration);然后,您可以对这些进行任何设置操作:

aset = set(zip(*([iter(a)] * 2)))
bset = set(zip(*([iter(b)] * 2)))
difference = bset - aset

演示:

>>> aset = set(zip(*([iter(a)] * 2)))
>>> bset = set(zip(*([iter(b)] * 2)))
>>> aset
{('another task', 'j'), ('cool task', 'b'), ('better task', 'y')}
>>> bset
{('a task', 'j'), ('another task', 'j'), ('cool task', 'b'), ('better task', 'y')}
>>> bset - aset
{('a task', 'j')}

答案 2 :(得分:1)

它可以按你的意愿工作:

#!/usr/bin/env python
# -*- coding: utf-8 -*-


def difference(a, b):
    a, b = (lambda x, y: (y, x) if len(set(x)) > len(set(y)) else (x, y)) (a, b)
    a_result = list(a)
    b_result = list(b)

    for z in range(len(a)):
        if a[z] in b:
            a_result.remove(a[z])
            b_result.remove(a[z])

    return a_result, b_result 
    # or
    # return a_result if len(set(a_result)) > len(set(b_result)) else b_result


def main():
    a = ['cool task', 'b', 'another task', 'j', 'better task', 'y']
    b = ['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better     task', 'y']
    print(difference(a, b))


if __name__ == "__main__":
    main()

答案 3 :(得分:0)

根据您的目的,您可以使用collections module中的Counter

>>> from collections import Counter
>>> a = Counter(['cool task', 'b', 'another task', 'j', 'better task', 'y'])
>>> b = Counter(['cool task', 'b', 'a task', 'j', 'another task', 'j', 'better task', 'y'])
>>> b-a
Counter({'j': 1, 'a task': 1})
>>> list((b-a).keys())
['j', 'a task']