在i列表数量中找到共同值

发布于 2025-01-25 21:18:12 字数 540 浏览 4 评论 0原文

我希望做上一个问题中描述的事情,而不是列表a,b,c我有i列表数(基于用户输入设置):

在多个列表中找到共同值

我如何从此问题中更改代码(如下所示)使用i列表号码?

>>> a = [1, 2, 3, 4]
>>> b = [2, 3, 4, 5, 6]
>>> c = [3, 4, 5, 6, 10, 12]
>>> elements_in_all = list(set.intersection(*map(set, [a, b, c])))
>>> elements_in_all
[3, 4]

我遇到的问题是我不知道我提前有多少列表!需要以某种方式可以

I wish to do what is described in this previous question but instead of lists a,b,c I have i number of lists (that is set based on user input):

Find common values in multiple lists

How can I change the code from this question (shown below) to work with i number of lists?

>>> a = [1, 2, 3, 4]
>>> b = [2, 3, 4, 5, 6]
>>> c = [3, 4, 5, 6, 10, 12]
>>> elements_in_all = list(set.intersection(*map(set, [a, b, c])))
>>> elements_in_all
[3, 4]

The issue I have is i don't know how many lists I have in advance! needs to be iterable somehow

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2025-02-01 21:18:12

如果从用户那里获得输入,请尝试将其存储在列表中,例如

lists = [[1,2,3], [1,2]]

,使用该变量而不是[a,b,c],在内部映射所有这些列表并将它们转换为设置。

li = [[1,2,3], [1,2]]
print(list(set.intersection(*map(set, li))))

if you get input from the user, try to store it in the list of lists,like

lists = [[1,2,3], [1,2]]

then, use that variable instead of [a, b, c], map internally iterates all these lists and convert them to set.

li = [[1,2,3], [1,2]]
print(list(set.intersection(*map(set, li))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文