创建多个列表中包含的所有值的并集的 Pythonic 方法

发布于 2024-08-19 09:30:01 字数 196 浏览 8 评论 0原文

我有一个列表列表:

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

我想展平这个列表并删除所有重复项;或者,换句话说,应用集合并运算:

desired_result = [1, 2, 3, 4, 5]

执行此操作的最简单方法是什么?

I have a list of lists:

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

I want to flatten this list and remove all duplicates; or, in other words, apply a set union operation:

desired_result = [1, 2, 3, 4, 5]

What's the easiest way to do this?

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

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

发布评论

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

评论(7

-小熊_ 2024-08-26 09:30:01

set.union 执行您的操作想要:

>>> results_list = [[1,2,3], [1,2,4]]
>>> results_union = set().union(*results_list)
>>> print(results_union)
set([1, 2, 3, 4])

您也可以使用两个以上的列表来执行此操作。

set.union does what you want:

>>> results_list = [[1,2,3], [1,2,4]]
>>> results_union = set().union(*results_list)
>>> print(results_union)
set([1, 2, 3, 4])

You can also do this with more than two lists.

新一帅帅 2024-08-26 09:30:01

由于您似乎使用的是 Python 2.5(如果您需要版本 != 2.6 的 A,顺便说一句,当前的生产版本,那么在 Q 中会很好提及;-)并且想要一个列表而不是结果集,我建议:

import itertools

...

return list(set(itertools.chain(*result_list)))

itertools 通常是使用迭代器(以及许多类型的序列或集合)的好方法,我衷心建议您熟悉它。 itertools.chain 特别记录在 这里

Since you seem to be using Python 2.5 (it would be nice to mention in your Q if you need an A for versions != 2.6, the current production one, by the way;-) and want a list rather than a set as the result, I recommend:

import itertools

...

return list(set(itertools.chain(*result_list)))

itertools is generally a great way to work with iterators (and so with many kinds of sequences or collections) and I heartily recommend you become familiar with it. itertools.chain, in particular, is documented here.

梦里南柯 2024-08-26 09:30:01

你也可以效仿这个风格

In [12]: a = ['Orange and Banana', 'Orange Banana']
In [13]: b = ['Grapes', 'Orange Banana']
In [14]: c = ['Foobanana', 'Orange and Banana']

In [20]: list(set(a) | set(b) | set(c))
Out[20]: ['Orange and Banana', 'Foobanana', 'Orange Banana', 'Grapes']

In [21]: list(set(a) & set(b) | set(c))
Out[21]: ['Orange and Banana', 'Foobanana', 'Orange Banana']    

You can also follow this style

In [12]: a = ['Orange and Banana', 'Orange Banana']
In [13]: b = ['Grapes', 'Orange Banana']
In [14]: c = ['Foobanana', 'Orange and Banana']

In [20]: list(set(a) | set(b) | set(c))
Out[20]: ['Orange and Banana', 'Foobanana', 'Orange Banana', 'Grapes']

In [21]: list(set(a) & set(b) | set(c))
Out[21]: ['Orange and Banana', 'Foobanana', 'Orange Banana']    
花伊自在美 2024-08-26 09:30:01

以理解方式:

[*{ j for i in lists for j in i }]

[*functools.reduce(lambda x,y: {*x, *y}, lists)]

in comprehension way:

[*{ j for i in lists for j in i }]

or

[*functools.reduce(lambda x,y: {*x, *y}, lists)]
带刺的爱情 2024-08-26 09:30:01

并集不受列表支持,列表是有序的,但集支持。查看 set.union

Unions are not supported by lists, which are ordered, but are supported by sets. Check out set.union.

挽袖吟 2024-08-26 09:30:01

我使用以下方法进行相交,这避免了对集合的需要。

a, b= [[1,2,3], [1,2]]
s = filter( lambda x: x in b, a)

或者,

s = [ x for x in b if x in a ]

I used the following to do intersections, which avoids the need for sets.

a, b= [[1,2,3], [1,2]]
s = filter( lambda x: x in b, a)

or,

s = [ x for x in b if x in a ]
菩提树下叶撕阳。 2024-08-26 09:30:01
desired_result = [x for y in lists for x in y]
desired_result = [x for y in lists for x in y]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文