Python3找出2个列表中有多少个差异才能相等 [英] Python3 find how many differences are in 2 lists in order to be equal

查看:154
本文介绍了Python3找出2个列表中有多少个差异才能相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有2个列表,总是,长度相同,总是包含字符串.

Assuming we got 2 lists, always with the same length and always containing strings.

list1 = ['sot', 'sot', 'ts', 'gg', 'gg', 'gg']
list2 = ['gg', 'gg', 'gg', 'gg', 'gg', 'sot']

我们需要找到:

list2的多少项应更改,以使其与list1 相等.

How many items of the list2 should change, in order for it to be equals with list1.

因此在上一个示例中,它应该返回2

So on the previous example it should return 2

对于此示例:

list1 = ['sot', 'sot', 'ts', 'gg', 'gg', 'gg']
list2 = ['gg', 'gg', 'gg', 'gg', 'sot', 'sot']

它应该返回1

最后是这个示例:

list1 = ['sot', 'sot', 'ts', 'gg', 'gg', 'gg']
list2 = ['ts', 'ts', 'ts', 'ts', 'ts', 'ts']

它应该返回5.

我们不在乎应该将哪些元素更改为什么.我们都不在乎顺序,所以这意味着

We do not care about which elements should change to what. We neither care about the order, so that means that

['gg', 'gg', 'gg', 'gg', 'gg', 'sot'] 
and
['gg', 'gg', 'sot', 'gg', 'gg', 'gg']

相等,它们的结果应为0.

are equal and the result of them should be 0.

列表的长度可以是6、8、20或其他,有时还会有更多的元素.

The length of the lists could be 6, 8, 20 or whatever and sometimes there are more elements in place.

我尝试了很多类似set(list1) - set(list2)list(set(list1).difference(list2))set(list1).symmetric_difference(set(list2))的方法,但是没有成功.

I tried a lot of things like set(list1) - set(list2) ,list(set(list1).difference(list2)) , set(list1).symmetric_difference(set(list2)) but without any success.

推荐答案

您可以利用许多可能性

You could leverage the many possibilities Counter offers:

list1 = ['sot', 'sot', 'ts', 'gg', 'gg', 'gg']
list2 = ['gg', 'gg', 'gg', 'gg', 'gg', 'sot']

from collections import Counter

sum((Counter(list1) - Counter(list2)).values())
# 2


让我们检查其他示例:


Lets check with the other examples:

list1 = ['sot', 'sot', 'ts', 'gg', 'gg', 'gg']
list2 = ['gg', 'gg', 'gg', 'gg', 'sot', 'sot']

sum((Counter(list1) - Counter(list2)).values())
# 1

list1 = ['sot', 'sot', 'ts', 'gg', 'gg', 'gg']
list2 = ['ts', 'ts', 'ts', 'ts', 'ts', 'ts']

sum((Counter(list1) - Counter(list2)).values())
# 5

list1 = ['gg', 'gg', 'gg', 'gg', 'gg', 'sot'] 
list2 = ['gg', 'gg', 'sot', 'gg', 'gg', 'gg']

sum((Counter(list1) - Counter(list2)).values())
# 0


详细信息

通过使用Counter,您将以字典的形式对每个列表中的所有元素进行计数.让我们回到第一个示例:

By using Counter, you will have a count of all elements from each list in the form of a dictionary. Lets go back to the first example:

c1 = Counter(list1)
# Counter({'sot': 2, 'ts': 1, 'gg': 3})

c2 = Counter(list2)
# Counter({'gg': 5, 'sot': 1})

现在我们想以某种方式了解以下内容:

Now we somehow would like to get an understanding of:

  • list1中存在哪些项目,但list2

  • Which items are present in list1 but not in list2

在存在的事物和不存在的事物中,list2需要多少个事物,以便它们包含相同数量的计数

Out of those that are present and also those there are not, how many more are needed in list2 so that they contain the same amount of counts

好吧,我们可以利用计数器支持数学运算这一事实,其结果产生multisets,即计数器的计数大于零.因此,鉴于我们正在寻找两个计数器之间的差异,因此我们似乎可以将它们相减,并查看list2中需要哪些元素及其各自的计数.

Well we could take advantage of the fact that counters support mathematical operations, the result of which produces multisets, i.e counters that have counts greater than zero. So given that we're looking for the difference between both counters it seems like we could subtract them and see what elements and their respective counts are needed in list2.

那么Counter之间的减法将如何工作?让我们用一个简单的例子检查一下:

So how would subtraction between Counters work? Lets check with a simple example:

Counter({1:4, 2: 1}) - Counter({1:1, 3:1})  
# Counter({1: 3, 2: 1})

因此,这样做是减去相应元素的计数,因此第一个计数器中包含的元素,因此这里的顺序很重要.因此,回到建议的示例,减去两个列表将得出:

So what this doing is subtracting the counts of corresponding elements, so the elements contained in the first counter, thus order here is important. So going back to the proposed example subtracting both lists would yield:

 sub = Counter(list1) - Counter(list2)
# Counter({'sot': 1, 'ts': 1})

现在,我们只需要简单地计算所有keys中的values,可以通过以下方式完成:

Now we simple need to count the values in all the keys, which can be done with:

sum(sub.values())
# 2

这篇关于Python3找出2个列表中有多少个差异才能相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆