All*_*len 32 python combinations list
编辑:这不是如何获得列表元素的所有可能组合的完全重复?
本主题是关于查找唯一组合,而另一个主题是关于查找所有组合.
如果我有一个python列表:
L = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
什么是获得列表中3个元素的所有可能独特组合的最佳方式,如下所示:
["1,2,3", "1,2,4", "2,3,4", "3,4,1"]
Run Code Online (Sandbox Code Playgroud)
组合中元素的顺序无关紧要.例如,"1,2,3"
与"3,2,1"
将被认为是相同的组合.
我可以写一些循环来做到这一点,但我认为可能有一个单行可以做同样的事情.
Ash*_*ary 43
>>> from itertools import combinations
>>> L = [1, 2, 3, 4]
>>> [",".join(map(str, comb)) for comb in combinations(L, 3)]
['1,2,3', '1,2,4', '1,3,4', '2,3,4']
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
34737 次 |
最近记录: |