获得两个列表之间的区别

Max*_*rai 731 python performance list set set-difference

我在Python中有两个列表,如下所示:

temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
Run Code Online (Sandbox Code Playgroud)

我需要创建第三个列表,其中包含第一个列表中不存在于第二个列表中的项目.从我必须得到的例子:

temp3 = ['Three', 'Four']
Run Code Online (Sandbox Code Playgroud)

有没有循环和检查的快速方法?

ars*_*ars 1110

In [5]: list(set(temp1) - set(temp2))
Out[5]: ['Four', 'Three']
Run Code Online (Sandbox Code Playgroud)

要小心

In [5]: set([1, 2]) - set([2, 3])
Out[5]: set([1]) 
Run Code Online (Sandbox Code Playgroud)

您可能期望/希望它相等的地方set([1, 3]).如果您确实想要set([1, 3])作为答案,则需要使用set([1, 2]).symmetric_difference(set([2, 3])).

  • 对称差异可以写成:^(set1 ^ set2) (34认同)
  • @Drewdin:列表不支持" - "操作数.但是,如果你仔细观察,那就设置,以及上面说明的内容. (18认同)
  • 请你编辑你的答案并指出这只会返回temp1-temp2吗?..正如其他人所说,为了返回所有差异,你必须使用sysmetric差异:list(set(temp1)^ set(temp2)) (9认同)
  • 请注意,由于集合是无序的,差值上的迭代器可以以任何顺序返回元素。例如,`list(set(temp1) - set(temp2)) == ['Four', 'Three']` **or** `list(set(temp1) - set(temp2)) == ['Three ', '四']`。 (6认同)
  • 如果有重复元素怎么办?例如 `a=[1, 1, 1, 1, 2, 2], b=[1, 1, 2, 2]` (5认同)
  • 此方法不保留输入列表的顺序。 (2认同)
  • 如果一个列表中有两个相等的元素,该解决方案将不起作用:示例:was [1,2,2],b was [1],预期 [2,2]:[2] 应等于 [2, 2 ] (2认同)

Mar*_*ers 451

现有的解决方案都提供以下一种或另一种:

  • 比O(n*m)性能更快.
  • 保留输入列表的顺序.

但到目前为止,没有任何解决方案.如果你想要两者,试试这个:

s = set(temp2)
temp3 = [x for x in temp1 if x not in s]
Run Code Online (Sandbox Code Playgroud)

性能测试

import timeit
init = 'temp1 = list(range(100)); temp2 = [i * 2 for i in range(50)]'
print timeit.timeit('list(set(temp1) - set(temp2))', init, number = 100000)
print timeit.timeit('s = set(temp2);[x for x in temp1 if x not in s]', init, number = 100000)
print timeit.timeit('[item for item in temp1 if item not in temp2]', init, number = 100000)
Run Code Online (Sandbox Code Playgroud)

结果:

4.34620224079 # ars' answer
4.2770634955  # This answer
30.7715615392 # matt b's answer
Run Code Online (Sandbox Code Playgroud)

我提出的方法以及保持顺序也比设置减法(稍微)快,因为它不需要构造不必要的集合.如果第一个列表比第二个列表长得多并且散列是昂贵的,则性能差异会更明显.这是第二次测试,证明了这一点:

init = '''
temp1 = [str(i) for i in range(100000)]
temp2 = [str(i * 2) for i in range(50)]
'''
Run Code Online (Sandbox Code Playgroud)

结果:

11.3836875916 # ars' answer
3.63890368748 # this answer (3 times faster!)
37.7445402279 # matt b's answer
Run Code Online (Sandbox Code Playgroud)

  • 对此答案的额外支持:了解保留列表顺序对性能很重要的用例.使用tarinfo或zipinfo对象时,我使用的是set subtraction.排除从存档中提取某些tarinfo对象.创建新列表速度很快,但在提取期间速度非常慢.起初的原因让我想起了.原来重新排序tarinfo对象列表造成了巨大的性能损失.切换到列表理解方法保存了一天. (2认同)
  • 如果list2拥有比list1更多的elemnet,它将无法工作 (2认同)
  • @haccks因为检查列表的成员资格是一个O(n)操作(迭代整个列表),但检查一个集合的成员资格是O(1). (2认同)

mat*_*t b 74

temp3 = [item for item in temp1 if item not in temp2]
Run Code Online (Sandbox Code Playgroud)

  • 将`temp2`变成一组之前会使这个效率更高一些. (12认同)
  • 评论说 (lists|tuples) 没有重复项。 (4认同)
  • 投票不需要列表项可以清除. (4认同)
  • 是的,取决于Ockonal是否关心重复(原始问题没有说) (3认同)
  • 我赞成你的回答,因为我一开始认为你对重复项的看法是正确的。但是“item not in temp2”和“item not in set(temp2)”将始终返回相同的结果,无论“temp2”中是否存在重复项。 (2认同)

aru*_*lmr 20

可以使用以下简单函数找到两个列表(例如list1和list2)之间的差异.

def diff(list1, list2):
    c = set(list1).union(set(list2))  # or c = set(list1) | set(list2)
    d = set(list1).intersection(set(list2))  # or d = set(list1) & set(list2)
    return list(c - d)
Run Code Online (Sandbox Code Playgroud)

要么

def diff(list1, list2):
    return list(set(list1).symmetric_difference(set(list2)))  # or return list(set(list1) ^ set(list2))
Run Code Online (Sandbox Code Playgroud)

通过使用上述功能,可以使用diff(temp2, temp1)或找到差异diff(temp1, temp2).两者都会给出结果['Four', 'Three'].您不必担心列表的顺序或首先给出的列表.

Python doc参考

  • 为什么不设置(list1).symmetric_difference(set(list2))? (7认同)

Sep*_*man 19

如果你想要递归的差异,我已经为python编写了一个包:https: //github.com/seperman/deepdiff

安装

从PyPi安装:

pip install deepdiff
Run Code Online (Sandbox Code Playgroud)

用法示例

输入

>>> from deepdiff import DeepDiff
>>> from pprint import pprint
>>> from __future__ import print_function # In case running on Python 2
Run Code Online (Sandbox Code Playgroud)

同一对象返回空

>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = t1
>>> print(DeepDiff(t1, t2))
{}
Run Code Online (Sandbox Code Playgroud)

项目类型已更改

>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = {1:1, 2:"2", 3:3}
>>> pprint(DeepDiff(t1, t2), indent=2)
{ 'type_changes': { 'root[2]': { 'newtype': <class 'str'>,
                                 'newvalue': '2',
                                 'oldtype': <class 'int'>,
                                 'oldvalue': 2}}}
Run Code Online (Sandbox Code Playgroud)

项目的价值已更改

>>> t1 = {1:1, 2:2, 3:3}
>>> t2 = {1:1, 2:4, 3:3}
>>> pprint(DeepDiff(t1, t2), indent=2)
{'values_changed': {'root[2]': {'newvalue': 4, 'oldvalue': 2}}}
Run Code Online (Sandbox Code Playgroud)

添加和/或删除项目

>>> t1 = {1:1, 2:2, 3:3, 4:4}
>>> t2 = {1:1, 2:4, 3:3, 5:5, 6:6}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff)
{'dic_item_added': ['root[5]', 'root[6]'],
 'dic_item_removed': ['root[4]'],
 'values_changed': {'root[2]': {'newvalue': 4, 'oldvalue': 2}}}
Run Code Online (Sandbox Code Playgroud)

字符串差异

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world"}}
>>> t2 = {1:1, 2:4, 3:3, 4:{"a":"hello", "b":"world!"}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{ 'values_changed': { 'root[2]': {'newvalue': 4, 'oldvalue': 2},
                      "root[4]['b']": { 'newvalue': 'world!',
                                        'oldvalue': 'world'}}}
Run Code Online (Sandbox Code Playgroud)

字符串差异2

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world!\nGoodbye!\n1\n2\nEnd"}}
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world\n1\n2\nEnd"}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{ 'values_changed': { "root[4]['b']": { 'diff': '--- \n'
                                                '+++ \n'
                                                '@@ -1,5 +1,4 @@\n'
                                                '-world!\n'
                                                '-Goodbye!\n'
                                                '+world\n'
                                                ' 1\n'
                                                ' 2\n'
                                                ' End',
                                        'newvalue': 'world\n1\n2\nEnd',
                                        'oldvalue': 'world!\n'
                                                    'Goodbye!\n'
                                                    '1\n'
                                                    '2\n'
                                                    'End'}}}

>>> 
>>> print (ddiff['values_changed']["root[4]['b']"]["diff"])
--- 
+++ 
@@ -1,5 +1,4 @@
-world!
-Goodbye!
+world
 1
 2
 End
Run Code Online (Sandbox Code Playgroud)

输入更改

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}}
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":"world\n\n\nEnd"}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{ 'type_changes': { "root[4]['b']": { 'newtype': <class 'str'>,
                                      'newvalue': 'world\n\n\nEnd',
                                      'oldtype': <class 'list'>,
                                      'oldvalue': [1, 2, 3]}}}
Run Code Online (Sandbox Code Playgroud)

列表差异

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3, 4]}}
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2]}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{'iterable_item_removed': {"root[4]['b'][2]": 3, "root[4]['b'][3]": 4}}
Run Code Online (Sandbox Code Playgroud)

清单差异2:

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}}
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{ 'iterable_item_added': {"root[4]['b'][3]": 3},
  'values_changed': { "root[4]['b'][1]": {'newvalue': 3, 'oldvalue': 2},
                      "root[4]['b'][2]": {'newvalue': 2, 'oldvalue': 3}}}
Run Code Online (Sandbox Code Playgroud)

列出差异忽略顺序或重复:(使用与上面相同的词典)

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, 3]}}
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 3, 2, 3]}}
>>> ddiff = DeepDiff(t1, t2, ignore_order=True)
>>> print (ddiff)
{}
Run Code Online (Sandbox Code Playgroud)

包含字典的列表:

>>> t1 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, {1:1, 2:2}]}}
>>> t2 = {1:1, 2:2, 3:3, 4:{"a":"hello", "b":[1, 2, {1:3}]}}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (ddiff, indent = 2)
{ 'dic_item_removed': ["root[4]['b'][2][2]"],
  'values_changed': {"root[4]['b'][2][1]": {'newvalue': 3, 'oldvalue': 1}}}
Run Code Online (Sandbox Code Playgroud)

集:

>>> t1 = {1, 2, 8}
>>> t2 = {1, 2, 3, 5}
>>> ddiff = DeepDiff(t1, t2)
>>> pprint (DeepDiff(t1, t2))
{'set_item_added': ['root[3]', 'root[5]'], 'set_item_removed': ['root[8]']}
Run Code Online (Sandbox Code Playgroud)

命名元组:

>>> from collections import namedtuple
>>> Point = namedtuple('Point', ['x', 'y'])
>>> t1 = Point(x=11, y=22)
>>> t2 = Point(x=11, y=23)
>>> pprint (DeepDiff(t1, t2))
{'values_changed': {'root.y': {'newvalue': 23, 'oldvalue': 22}}}
Run Code Online (Sandbox Code Playgroud)

自定义对象:

>>> class ClassA(object):
...     a = 1
...     def __init__(self, b):
...         self.b = b
... 
>>> t1 = ClassA(1)
>>> t2 = ClassA(2)
>>> 
>>> pprint(DeepDiff(t1, t2))
{'values_changed': {'root.b': {'newvalue': 2, 'oldvalue': 1}}}
Run Code Online (Sandbox Code Playgroud)

添加了对象属性:

>>> t2.c = "new attribute"
>>> pprint(DeepDiff(t1, t2))
{'attribute_added': ['root.c'],
 'values_changed': {'root.b': {'newvalue': 2, 'oldvalue': 1}}}
Run Code Online (Sandbox Code Playgroud)


den*_*ufa 14

如果你真的在研究性能,那么使用numpy!

这是完整的笔记本作为github上的要点,比较list,numpy和pandas.

https://gist.github.com/denfromufa/2821ff59b02e9482be15d27f2bbd4451

在此输入图像描述


Moh*_*med 14

最简单的方法,

使用set().difference(set())

list_a = [1,2,3]
list_b = [2,3]
print set(list_a).difference(set(list_b))
Run Code Online (Sandbox Code Playgroud)

答案是 set([1])

可以打印为列表,

print list(set(list_a).difference(set(list_b)))
Run Code Online (Sandbox Code Playgroud)

  • 删除重复项并且不保留顺序 (2认同)

aar*_*ing 13

因为现有的解决方案都没有产生元组,所以我会抛弃:

temp3 = tuple(set(temp1) - set(temp2))
Run Code Online (Sandbox Code Playgroud)

或者:

#edited using @Mark Byers idea. If you accept this one as answer, just accept his instead.
temp3 = tuple(x for x in temp1 if x not in set(temp2))
Run Code Online (Sandbox Code Playgroud)

就像其他非元组在这个方向上产生答案一样,它保留了秩序


Sup*_*ova 13

可以使用python XOR运算符完成.

  • 这将删除每个列表中的重复项
  • 这将显示temp1与temp1和temp2与temp1的区别.

set(temp1) ^ set(temp2)
Run Code Online (Sandbox Code Playgroud)

  • 最佳答案! (10认同)
  • 这是怎么埋葬的……很棒的电话 (5认同)
  • 这是 2 边差异的最佳选择 (4认同)
  • @Gangula要查看两种方法之间的差异,请向“temp2”添加一个“temp1”中不存在的值,然后重试。 (3认同)
  • 这是否比任何其他解决方案在时间上表现更好? (2认同)

are*_*lek 11

我想要的东西,将采取两个列表,并可以做什么diffbash呢.因为当你搜索"python diff two lists"并且不是非常具体时会弹出这个问题,我会发布我想出的内容.

使用SequenceMatherdifflib您可以比较两个列表喜欢diff做.其他答案都没有告诉你差异发生的位置,但这个答案会发生.一些答案仅在一个方向上产生差异.有些人重新排序元素.有些人不处理重复.但是这个解决方案给你两个列表之间的真正区别:

a = 'A quick fox jumps the lazy dog'.split()
b = 'A quick brown mouse jumps over the dog'.split()

from difflib import SequenceMatcher

for tag, i, j, k, l in SequenceMatcher(None, a, b).get_opcodes():
  if tag == 'equal': print('both have', a[i:j])
  if tag in ('delete', 'replace'): print('  1st has', a[i:j])
  if tag in ('insert', 'replace'): print('  2nd has', b[k:l])
Run Code Online (Sandbox Code Playgroud)

这输出:

both have ['A', 'quick']
  1st has ['fox']
  2nd has ['brown', 'mouse']
both have ['jumps']
  2nd has ['over']
both have ['the']
  1st has ['lazy']
both have ['dog']
Run Code Online (Sandbox Code Playgroud)

当然,如果您的应用程序做出与其他答案相同的假设,您将从中受益最多.但如果您正在寻找真正的diff功能,那么这是唯一的出路.

例如,其他答案都无法处理:

a = [1,2,3,4,5]
b = [5,4,3,2,1]
Run Code Online (Sandbox Code Playgroud)

但是这个做了:

  2nd has [5, 4, 3, 2]
both have [1]
  1st has [2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)


Mac*_*arz 9

试试这个:

temp3 = set(temp1) - set(temp2)
Run Code Online (Sandbox Code Playgroud)


小智 9

这可能比马克的列表理解速度更快:

list(itertools.filterfalse(set(temp2).__contains__, temp1))
Run Code Online (Sandbox Code Playgroud)

  • 可能想在这里包含`from itertools import filterfalse`位.另请注意,这不会返回与其他序列类似的序列,它会返回迭代器. (5认同)

sre*_*gam 7

arulmr解决方案的单行版本

def diff(listA, listB):
    return set(listA) - set(listB) | set(listB) -set(listA)
Run Code Online (Sandbox Code Playgroud)


Tay*_*ton 6

这是Counter最简单案例的答案.

这比上面做双向差异的要短,因为它只完成问题所要求的:生成第一个列表中的内容而不是第二个列表.

from collections import Counter

lst1 = ['One', 'Two', 'Three', 'Four']
lst2 = ['One', 'Two']

c1 = Counter(lst1)
c2 = Counter(lst2)
diff = list((c1 - c2).elements())
Run Code Online (Sandbox Code Playgroud)

或者,根据您的可读性偏好,它可以提供一个像样的单行:

diff = list((Counter(lst1) - Counter(lst2)).elements())
Run Code Online (Sandbox Code Playgroud)

输出:

['Three', 'Four']
Run Code Online (Sandbox Code Playgroud)

请注意,list(...)如果您只是迭代它,则可以删除该调用.

因为此解决方案使用计数器,所以它与许多基于集合的答案相比正确处理数量.例如,在此输入上:

lst1 = ['One', 'Two', 'Two', 'Two', 'Three', 'Three', 'Four']
lst2 = ['One', 'Two']
Run Code Online (Sandbox Code Playgroud)

输出是:

['Two', 'Two', 'Three', 'Three', 'Four']
Run Code Online (Sandbox Code Playgroud)


Jen*_*obi 6

假设我们有两个列表

list1 = [1, 3, 5, 7, 9]
list2 = [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)

从上面两个列表中我们可以看到,list2 中存在第 1、3、5 项,而第 7、9 项则不存在。另一方面,列表 1 中存在项目 1、3、5,而项目 2、4 则不存在。

返回包含项目 7、9 和 2、4 的新列表的最佳解决方案是什么?

上面的所有答案都找到了解决方案,那么现在什么是最佳的呢?

def difference(list1, list2):
    new_list = []
    for i in list1:
        if i not in list2:
            new_list.append(i)

    for j in list2:
        if j not in list1:
            new_list.append(j)
    return new_list
Run Code Online (Sandbox Code Playgroud)

相对

def sym_diff(list1, list2):
    return list(set(list1).symmetric_difference(set(list2)))
Run Code Online (Sandbox Code Playgroud)

使用timeit我们可以看到结果

t1 = timeit.Timer("difference(list1, list2)", "from __main__ import difference, 
list1, list2")
t2 = timeit.Timer("sym_diff(list1, list2)", "from __main__ import sym_diff, 
list1, list2")

print('Using two for loops', t1.timeit(number=100000), 'Milliseconds')
print('Using two for loops', t2.timeit(number=100000), 'Milliseconds')
Run Code Online (Sandbox Code Playgroud)

回报

[7, 9, 2, 4]
Using two for loops 0.11572412995155901 Milliseconds
Using symmetric_difference 0.11285737506113946 Milliseconds

Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)


Abh*_*k S 6

这是@SuperNova答案的修改版本

def get_diff(a: list, b: list) -> list:
    return list(set(a) ^ set(b))
Run Code Online (Sandbox Code Playgroud)


小智 5

这是另一种解决方案:

def diff(a, b):
    xa = [i for i in set(a) if i not in b]
    xb = [i for i in set(b) if i not in a]
    return xa + xb
Run Code Online (Sandbox Code Playgroud)


sou*_*ner 5

如果对difflist的元素进行排序和设置,则可以使用朴素方法.

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

print list1[len(list2):]
Run Code Online (Sandbox Code Playgroud)

或使用本机设置方法:

subset=set(list1).difference(list2)

print subset

import timeit
init = 'temp1 = list(range(100)); temp2 = [i * 2 for i in range(50)]'
print "Naive solution: ", timeit.timeit('temp1[len(temp2):]', init, number = 100000)
print "Native set solution: ", timeit.timeit('set(temp1).difference(temp2)', init, number = 100000)
Run Code Online (Sandbox Code Playgroud)

天真的解决方案:0.0787101593292

原生集解决方案:0.998837615564


fif*_*nce 5

如果您遇到TypeError: unhashable type: 'list'需要将列表或集合转换为元组的情况,例如

set(map(tuple, list_of_lists1)).symmetric_difference(set(map(tuple, list_of_lists2)))
Run Code Online (Sandbox Code Playgroud)

另请参阅如何在 python 中比较列表/集合的列表?


小智 5

为此我在游戏中为时不晚,但是您可以将上述某些代码的性能与此进行比较,其中两个最快的竞争者是:

list(set(x).symmetric_difference(set(y)))
list(set(x) ^ set(y))
Run Code Online (Sandbox Code Playgroud)

对于基本的编码我深表歉意。

import time
import random
from itertools import filterfalse

# 1 - performance (time taken)
# 2 - correctness (answer - 1,4,5,6)
# set performance
performance = 1
numberoftests = 7

def answer(x,y,z):
    if z == 0:
        start = time.clock()
        lists = (str(list(set(x)-set(y))+list(set(y)-set(y))))
        times = ("1 = " + str(time.clock() - start))
        return (lists,times)

    elif z == 1:
        start = time.clock()
        lists = (str(list(set(x).symmetric_difference(set(y)))))
        times = ("2 = " + str(time.clock() - start))
        return (lists,times)

    elif z == 2:
        start = time.clock()
        lists = (str(list(set(x) ^ set(y))))
        times = ("3 = " + str(time.clock() - start))
        return (lists,times)

    elif z == 3:
        start = time.clock()
        lists = (filterfalse(set(y).__contains__, x))
        times = ("4 = " + str(time.clock() - start))
        return (lists,times)

    elif z == 4:
        start = time.clock()
        lists = (tuple(set(x) - set(y)))
        times = ("5 = " + str(time.clock() - start))
        return (lists,times)

    elif z == 5:
        start = time.clock()
        lists = ([tt for tt in x if tt not in y])
        times = ("6 = " + str(time.clock() - start))
        return (lists,times)

    else:    
        start = time.clock()
        Xarray = [iDa for iDa in x if iDa not in y]
        Yarray = [iDb for iDb in y if iDb not in x]
        lists = (str(Xarray + Yarray))
        times = ("7 = " + str(time.clock() - start))
        return (lists,times)

n = numberoftests

if performance == 2:
    a = [1,2,3,4,5]
    b = [3,2,6]
    for c in range(0,n):
        d = answer(a,b,c)
        print(d[0])

elif performance == 1:
    for tests in range(0,10):
        print("Test Number" + str(tests + 1))
        a = random.sample(range(1, 900000), 9999)
        b = random.sample(range(1, 900000), 9999)
        for c in range(0,n):
            #if c not in (1,4,5,6):
            d = answer(a,b,c)
            print(d[1])
Run Code Online (Sandbox Code Playgroud)


pyl*_*ang 5

这是区分两个字符串列表的几种简单的,保留顺序的方法。

一种不寻常的方法,使用pathlib

import pathlib


temp1 = ["One", "Two", "Three", "Four"]
temp2 = ["One", "Two"]

p = pathlib.Path(*temp1)
r = p.relative_to(*temp2)
list(r.parts)
# ['Three', 'Four']
Run Code Online (Sandbox Code Playgroud)

假设两个列表都包含以相同的开头的字符串。有关更多详细信息,请参阅文档。注意,与设置操作相比,它并不是特别快。


使用以下方法的直接实现itertools.zip_longest

import itertools as it


[x for x, y in it.zip_longest(temp1, temp2) if x != y]
# ['Three', 'Four']
Run Code Online (Sandbox Code Playgroud)


小智 5

我更喜欢使用转换为集合,然后使用“difference()”函数。完整代码是:

temp1 = ['One', 'Two', 'Three', 'Four'  ]                   
temp2 = ['One', 'Two']
set1 = set(temp1)
set2 = set(temp2)
set3 = set1.difference(set2)
temp3 = list(set3)
print(temp3)

Run Code Online (Sandbox Code Playgroud)

输出:

>>>print(temp3)
['Three', 'Four']
Run Code Online (Sandbox Code Playgroud)

这是最容易理解的,而且将来如果您处理大数据,如果不需要重复项,将其转换为集将删除重复项。希望能帮助到你 ;-)


小智 5

我知道这个问题已经得到了很好的答案,但我希望使用添加以下方法numpy

temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']

list(np.setdiff1d(temp1,temp2))

['Four', 'Three'] #Output
Run Code Online (Sandbox Code Playgroud)


Oks*_*a B 5

如果您应该从列表a中删除列表b中存在的所有值。

def list_diff(a, b):
    r = []

    for i in a:
        if i not in b:
            r.append(i)
    return r
Run Code Online (Sandbox Code Playgroud)

list_diff([1,2,2],[1])

结果:[2,2]

或者

def list_diff(a, b):
    return [x for x in a if x not in b]
Run Code Online (Sandbox Code Playgroud)