[英]Difference Between Two Lists with Duplicates in Python
I have two lists that contain many of the same items, including duplicate items.我有两个列表,其中包含许多相同的项目,包括重复的项目。 I want to check which items in the first list are not in the second list.我想检查第一个列表中的哪些项目不在第二个列表中。 For example, I might have one list like this:例如,我可能有一个这样的列表:
l1 = ['a', 'b', 'c', 'b', 'c']
and one list like this:和一个这样的列表:
l2 = ['a', 'b', 'c', 'b']
Comparing these two lists I would want to return a third list like this:比较这两个列表,我想返回这样的第三个列表:
l3 = ['c']
I am currently using some terrible code that I made a while ago that I'm fairly certain doesn't even work properly shown below.我目前正在使用我不久前编写的一些糟糕的代码,我相当肯定它甚至不能正常工作,如下所示。
def list_difference(l1,l2):
for i in range(0, len(l1)):
for j in range(0, len(l2)):
if l1[i] == l1[j]:
l1[i] = 'damn'
l2[j] = 'damn'
l3 = []
for item in l1:
if item!='damn':
l3.append(item)
return l3
How can I better accomplish this task?我怎样才能更好地完成这项任务?
You didn't specify if the order matters.您没有说明订单是否重要。 If it does not, you can do this in >= Python 2.7:如果没有,您可以在 >= Python 2.7 中执行此操作:
l1 = ['a', 'b', 'c', 'b', 'c']
l2 = ['a', 'b', 'c', 'b']
from collections import Counter
c1 = Counter(l1)
c2 = Counter(l2)
diff = c1-c2
print list(diff.elements())
To take into account both duplicates and the order of elements:要同时考虑重复项和元素的顺序:
from collections import Counter
def list_difference(a, b):
count = Counter(a) # count items in a
count.subtract(b) # subtract items that are in b
diff = []
for x in a:
if count[x] > 0:
count[x] -= 1
diff.append(x)
return diff
print(list_difference("z y z x v x y x u".split(), "x y z w z".split()))
# -> ['y', 'x', 'v', 'x', 'u']
Python 2.5 version: Python 2.5 版本:
from collections import defaultdict
def list_difference25(a, b):
# count items in a
count = defaultdict(int) # item -> number of occurrences
for x in a:
count[x] += 1
# subtract items that are in b
for x in b:
count[x] -= 1
diff = []
for x in a:
if count[x] > 0:
count[x] -= 1
diff.append(x)
return diff
Counters are new in Python 2.7.计数器是 Python 2.7 中的新功能。 For a general solution to substract a from b:对于从 b 中减去 a 的一般解决方案:
def list_difference(b, a):
c = list(b)
for item in a:
try:
c.remove(item)
except ValueError:
pass #or maybe you want to keep a values here
return c
你可以试试这个
list(filter(lambda x:l1.remove(x),li2)) print(l1)
Try this one:试试这个:
from collections import Counter
from typing import Sequence
def duplicates_difference(a: Sequence, b: Sequence) -> Counter:
"""
>>> duplicates_difference([1,2],[1,2,2,3])
Counter({2: 1, 3: 1})
"""
shorter, longer = sorted([a, b], key=len)
return Counter(longer) - Counter(shorter)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.