【发布时间】:2014-06-21 06:40:29
【问题描述】:
from itertools import combinations
a = [1,2,3]
combinations(a,2) #will give me ((1,2),(1,3),(2,3))
combinations(a,3) #will give me ((1,2,3),)
但是如果我想要数组中不同长度的结果怎么办 例如
我想找到长度大于或等于 2 的给定数组 [1,2,3] 的所有组合
所以结果应该是((1,2),(1,3),(2,3),(1,2,3))
类似c = combinations(a,>=2)
我尝试使用 lambda 但它不起作用
c = combinations(a,lambda x: x for x in [2,3])
同时列出综合c = combinations(a,[x for x in [2,3]])
我知道我可以使用一个简单的循环,然后找出 diff 长度的组合。
for l in [2,3]:
combinations(a,l)
但是有什么pythonic方法可以做到这一点吗?
【问题讨论】:
标签: python python-2.7 python-3.x combinations itertools