简体   繁体   English

如何通过比较 python 中的两个列表来删除公共元素

[英]How to remove the common elements by comparing both lists in python

A = [int(x) for x in input('Enter your elements: ').split()]
B = [int(y) for y in input('Enter your elements: ').split()]

for i in A:
    if i in B:
        A.remove(i)
print(A)

#Here I will give 4 1 3 2 in Array A

#And In Array B I will give 3 1

#Hence there is 3 1 common between 2 arrays

#I want to return 4 2 as output

Let's try this:让我们试试这个:

>>> A = [int(x) for x in input('Enter your elements: ').split()]
Enter your elements: 5 6 7 8 9
>>> B = [int(y) for y in input('Enter your elements: ').split()]
Enter your elements: 1 2 3 4 5 6
>>> for i in A:
...     if i in B:
...         A.remove(i)
...
>>> A
[6, 7, 8, 9]
>>>

Now, you would expect A to be [7, 8, 9] , but modifying a list while iterating over it is a really bad idea.现在,您会期望A[7, 8, 9] ,但是迭代列表时修改列表是一个非常糟糕的主意。

We can see what's going on with some simple debugging with print .我们可以通过print进行一些简单的调试来查看发生了什么。

>>> a = [5, 6, 7, 8, 9]
>>> b = [1, 2, 3, 4, 5, 6]
>>> for x in a:
...   print(x)
...   if x in b:
...     a.remove(x)
...
5
7
8
9
>>> a
[6, 7, 8, 9]
>>>

Because 5 is removed from A , 6 is now the first element in A but we've moved past that, so the next element evaluated is 7 , and 6 never has a chance to be removed from A .因为5已从A中删除,所以6现在是A中的第一个元素,但我们已经移过了该元素,因此评估的下一个元素是7 ,并且6永远没有机会从A中删除。

Rather, let's generate a new list with the difference.相反,让我们生成一个具有差异的新列表

>>> A = [int(x) for x in input('Enter your elements: ').split()]
Enter your elements: 5 6 7 8 9
>>> B = [int(y) for y in input('Enter your elements: ').split()]
Enter your elements: 1 2 3 4 5 6
>>> C = [x for x in A if not x in B]
>>> C
[7, 8, 9]
>>>

That looks more like what you expected.这看起来更像您的预期。

Note that A and B are unmodified.请注意, AB未修改。 Also note that generally variables begin with lowercase letters in Python.另请注意,通常变量在 Python 中以小写字母开头。

Should you wish to modify one of the lists, you can simply assign the new list back to it.如果您希望修改其中一个列表,您只需将新列表重新分配给它即可。

>>> a = [5, 6, 7, 8, 9]
>>> b = [1, 2, 3, 4, 5, 6]
>>> a = [x for x in a if not x in b]
>>> a
[7, 8, 9]
>>>

Sets are the most pythonic way to handle "Find me items that are in this list/not in this other list", if the elements are guaranteed to be unique.如果保证元素是唯一的,则集合是处理“查找此列表中/不在此其他列表中的项目”的最 Pythonic 方式。

shared = set(A).intersection(set(B))
return_list = list(set(A)-shared)
return return_list

You can use set functionality to determine the intersection then remove elements as appropriate.您可以使用设置功能来确定交集,然后根据需要删除元素。

This technique ensure that duplicate values in list A that are not in list B will be retained.此技术可确保保留列表 A 中不在列表 B 中的重复值。

The removal is carried out in situ .移除是在原地进行的。

Order of list A is maintained.列表 A 的顺序保持不变。

A = [10,11,11,1,2,3,4,4]
B = [2, 4]

for i in set(A).intersection(B):
    while i in A:
        A.remove(i)

print(A)

Output: Output:

[10, 11, 11, 1, 3]

Here's an alternative without using sets:这是不使用集合的替代方法:

from collections import Counter

A = [10,11,11,1,2,3,4,4]
B = [2, 4]

counter = Counter(A)

for e in B:
    for _ in range(counter.get(e, 0)):
        A.remove(e)

print(A)

Just use the symmetric difference operator with sets:只需将对称差分运算符与集合一起使用:

filtered_list = list(set(A) ^ set(B))

Just use list comprehension and assign to A :只需使用列表理解并分配给A

A = [int(x) for x in input('Enter your elements: ').split()]
B = [int(y) for y in input('Enter your elements: ').split()]
A = [i for i in A if i not in B]
print(A)

# Enter your elements: 4 1 3 2
# Enter your elements: 3 1
# [4, 2]

But if you really need to modify original A , you can use this:但是如果你真的需要修改原来A ,你可以使用这个:

A[:] = [i for i in A if i not in B]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM