Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 元素相同的两个列表中的公共元素_Python_List_Intersection - Fatal编程技术网

Python 元素相同的两个列表中的公共元素

Python 元素相同的两个列表中的公共元素,python,list,intersection,Python,List,Intersection,我有以下两个列表: a=['not','not','not','not'] b=['not','not'] 我必须找到包含上述两个列表的中间部分的列表的len,结果是: intersection=['not','not'] len(intersection) 2 现在的问题是,我尝试了filter(a,b中的lambda x:x)和filter(b,a中的lambda x:x),但当两个列表中的一个比另一个长时,我没有得到交集,只是进行了成员身份检查。在上面的例子中,因为a的所有成员都在b中

我有以下两个列表:

a=['not','not','not','not']
b=['not','not']
我必须找到包含上述两个列表的中间部分的列表的
len
,结果是:

intersection=['not','not']
len(intersection)
2
现在的问题是,我尝试了
filter(a,b中的lambda x:x)
filter(b,a中的lambda x:x)
,但当两个列表中的一个比另一个长时,我没有得到交集,只是进行了成员身份检查。在上面的例子中,因为a的所有成员都在b中,所以我得到了4的公共元素的len;我想要的是交叉点,它是
len
2。 使用
set().intersection(set())
将创建一个集合,这不是我想要的,因为所有元素都是相同的。
你能给我推荐一些有价值的紧凑型解决方案吗?

我看不到任何特别紧凑的计算方法。让我们先找个解决办法

交叉点
是较短列表的某个子列表(例如
b
)。现在,为了在短列表不是非常短的情况下获得更好的性能,请将长列表设置为一个集合(例如
set(a)
)。交叉点可以表示为短列表中的项目的列表理解,短列表中的项目也在长列表中:

def common_elements(a, b):
    shorter, longer = (a, b) if len(a)<len(b) else (b, a)
    longer = set(longer)
    intersection = [item for item in shorter if item in longer]
    return intersection

a = ['not','not','not','not']
b = ['not','not']
print(common_elements(a,b))
def公共_元素(a,b):

更短,更长=(a,b)如果len(a)我看不到任何特别紧凑的计算方法。让我们先找个解决办法

交叉点
是较短列表的某个子列表(例如
b
)。现在,为了在短列表不是非常短的情况下获得更好的性能,请将长列表设置为一个集合(例如
set(a)
)。交叉点可以表示为短列表中的项目的列表理解,短列表中的项目也在长列表中:

def common_elements(a, b):
    shorter, longer = (a, b) if len(a)<len(b) else (b, a)
    longer = set(longer)
    intersection = [item for item in shorter if item in longer]
    return intersection

a = ['not','not','not','not']
b = ['not','not']
print(common_elements(a,b))
def公共_元素(a,b):

短,长=(a,b)如果len(a)通过
set
进行设置。首先将这些列表设置为集合,然后取它们的交点。现在交叉路口可能会有重复。因此,对于交叉点中的每个元素,取
a
b
中的最小重复次数

>>> a=['not','not','not','not']
>>> b=['not','not']
>>> def myIntersection(A,B):
...     setIn = set(A).intersection(set(B))
...     rt = []
...     for i in setIn:
...         for j in range(min(A.count(i),B.count(i))):
...             rt.append(i)
...     return rt
...
>>> myIntersection(a,b)
['not', 'not']

通过
set
进行设置。首先将这些列表设置为集合,然后取它们的交点。现在交叉路口可能会有重复。因此,对于交叉点中的每个元素,取
a
b
中的最小重复次数

>>> a=['not','not','not','not']
>>> b=['not','not']
>>> def myIntersection(A,B):
...     setIn = set(A).intersection(set(B))
...     rt = []
...     for i in setIn:
...         for j in range(min(A.count(i),B.count(i))):
...             rt.append(i)
...     return rt
...
>>> myIntersection(a,b)
['not', 'not']
如果您不介意使用,那么您可以使用以下解决方案

>>> import collections
>>> a=['not','not','not','not']
>>> b=['not','not']

>>> c1 = collections.Counter(a)
>>> c2 = collections.Counter(b)
然后按“not”索引

>>> c1['not'] + c2['not']
6
对于十字路口,您需要

>>> (c1 & c2) ['not']
2
如果您不介意使用,那么您可以使用以下解决方案

>>> import collections
>>> a=['not','not','not','not']
>>> b=['not','not']

>>> c1 = collections.Counter(a)
>>> c2 = collections.Counter(b)
然后按“not”索引

>>> c1['not'] + c2['not']
6
对于十字路口,您需要

>>> (c1 & c2) ['not']
2

您是否考虑过以下方法

a = ['not','not','not','not']
b = ['not','not']

min(len(a), len(b))
# 2

因为所有元素都是相同的,所以公共元素的数量只是两个列表长度中的最小值。

您考虑过以下方法吗

a = ['not','not','not','not']
b = ['not','not']

min(len(a), len(b))
# 2

由于所有元素都是相同的,所以公共元素的数量只是两个列表长度的最小值。

我认为
计数器在这里可能有用,但OP需要2个,而不是6个。我想的更像是
(计数器(a)和计数器(b))[“不是”]
。(或者sum((Counter(a)&Counter(b)).values())如果我们不想硬编码这个词。)@DSM
sum((Counter(a)&Counter(b)).values())
如果它们有多个词的话就会断开,不是吗?我还以为只有一个词呢?如果有多个单词,那么您将得到我认为是正确的泛化,即
计数器(“aabc”)和计数器(“ac”)==计数器({'a':1,'c':1})
。我认为
计数器在这里可能很有用,但OP需要2,而不是6。我想的更像是
(计数器(a)和计数器(b))[“不是”]
。(或者sum((Counter(a)&Counter(b)).values())如果我们不想硬编码这个词。)@DSM
sum((Counter(a)&Counter(b)).values())
如果它们有多个词的话就会断开,不是吗?我还以为只有一个词呢?如果有多个单词,那么你会得到我认为是正确的概括,即
计数器(“aabc”)和计数器(“ac”)==计数器({'a':1,'c':1})
。我的意思是
短,长=排序([a,b],key=len)
,但这并没有我想象的那么好。。。但无论如何……我的意思是
shorter,longer=sorted([a,b],key=len)
但这并不像我想象的那么好。。。但无论如何。。。