【发布时间】:2014-09-23 09:16:48
【问题描述】:
我需要长度为 k 的 0,1 的所有可能组合。
假设 k=2 我想要(0,0), (0,1), (1,0), (1,1)
我在itertools 中尝试了不同的功能,但没有找到我想要的。
>>> list(itertools.combinations_with_replacement([0,1], 2))
[(0, 0), (0, 1), (1, 1)]
>>> list(itertools.product([0,1], [0,1])) #does not work if k>2
[(0, 0), (0, 1), (1, 0), (1, 1)]
【问题讨论】:
-
快速是指计算上的快速:-)
标签: python combinations itertools