相关疑难解决方法(0)

可变数量列表的交集

我定义了两个列表的交集,如下所示:

def intersect(a, b):
  return list(set(a) & set(b))
Run Code Online (Sandbox Code Playgroud)

对于三个参数,它看起来像:

def intersect(a, b, c):
  return (list(set(a) & set(b) & set(c))
Run Code Online (Sandbox Code Playgroud)

我可以针对可变数量的列表推广此函数吗?

电话会看起来像:

>> intersect([1, 2, 2], [2, 3, 2], [2, 5, 2], [2, 7, 2])
[2]
Run Code Online (Sandbox Code Playgroud)

编辑:Python只能这样实现吗?

intersect([
          [1, 2, 2], [2, 3, 2], [2, 5, 2], [2, 7, 2]
         ])
[2]
Run Code Online (Sandbox Code Playgroud)

python intersection function list set

4
推荐指数
1
解决办法
3218
查看次数

标签 统计

function ×1

intersection ×1

list ×1

python ×1

set ×1