清单理解和交集问题 [英] List comprehension and intersection problem

查看:83
本文介绍了清单理解和交集问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

list(set(a[0]) & set(a[1]) & set(a[2]) & set(a[3]) & set(a[4]))

有人知道如何编写这种方式,这样我们就不必先验地知道我们将得到多少个列表? (即5个未硬编码)?

Does anyone know how to write this in a way such that we dont need to know apriori how many lists we will be given? (ie 5 not hard coded in)?

每个a都是大小不同的列表.

Each a is a list of varying size.

推荐答案

只要有至少一套,就可以这样做:

As long as you have at least one set, you can do this:

list(set(a[0]).intersection(*a[1:]))

如果可能没有集合,则必须自己决定无集合的交集"在您的应用程序中实际上意味着什么.如果要空集:

If there might be no sets, you have to decide for yourself what the "intersection of no sets" actually should mean in your application. If you want the empty set:

list(set(*a[:1]).intersection(*a[1:]))

这篇关于清单理解和交集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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