获取排序组合

发布于 2024-10-30 06:58:31 字数 445 浏览 4 评论 0原文

我有一个输入

A = [2,0,1,3,2,2,0,1,1,2,0].

,如下所示,我删除了 A 的所有重复项

A = list(Set(A))

,现在是 [0,1,2,3]。现在我想要用这个列表制作的所有对组合,但是它们不需要是唯一的......因此 [0,3] 等于 [3,0] 和 [2,3] 等于 [3,2]。在此示例中,它应该返回

[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]

How do I Achieve this?我查看了 iteratools 库。但无法想出解决办法。

I have a input like

A = [2,0,1,3,2,2,0,1,1,2,0].

Following I remove all the duplicates by

A = list(Set(A))

A is now [0,1,2,3]. Now I want all the pair combinations that I can make with this list, however they do not need to be unique... thus [0,3] equals [3,0] and [2,3] equals [3,2]. In this example it should return

[[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]]

How do I achieve this? I looked in the iteratools lib. But couldn't come up with a solution.

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

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

发布评论

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

评论(1

幻想少年梦 2024-11-06 06:58:31
>>> A = [2,0,1,3,2,2,0,1,1,2,0]
>>> A = sorted(set(A))   # list(set(A)) is not usually in order
>>> from itertools import combinations
>>> list(combinations(A, 2))
[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]

>>> map(list, combinations(A, 2))
[[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]

>>> help(combinations)
Help on class combinations in module itertools:

class combinations(__builtin__.object)
 |  combinations(iterable, r) --> combinations object
 |  
 |  Return successive r-length combinations of elements in the iterable.
 |  
 |  combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |  
 |  next(...)
 |      x.next() -> the next value, or raise StopIteration
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
>>> A = [2,0,1,3,2,2,0,1,1,2,0]
>>> A = sorted(set(A))   # list(set(A)) is not usually in order
>>> from itertools import combinations
>>> list(combinations(A, 2))
[(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]

>>> map(list, combinations(A, 2))
[[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]

>>> help(combinations)
Help on class combinations in module itertools:

class combinations(__builtin__.object)
 |  combinations(iterable, r) --> combinations object
 |  
 |  Return successive r-length combinations of elements in the iterable.
 |  
 |  combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |  
 |  next(...)
 |      x.next() -> the next value, or raise StopIteration
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文