我已经编写了一个简单的python程序的所有组合:的Python itertools.combinations_with_replacement不返回
#!/usr/bin/env python3.5
import sys, itertools
sCharacters = '123'
for iCombinationLength in range(0, len(sCharacters)+1):
for aCombination in itertools.combinations_with_replacement(sCharacters, iCombinationLength):
print(''.join(aCombination))
此输出以下:
1
2
3
11
12
13
22
23
33
111
112
113
122
123
133
222
223
233
333
然而它是数目的所有组合1 2和3,它将需要包括:
311
312
313
321
322
323
331
332
333
正如你可以看到上面,它确实不是。我已经看到其他文章给出了combination_with_replacement函数给出的解决方案,以获得所有可能的字符组合传递。但这似乎并没有发生。我在这里做错了什么,以及如何获得字符变量中所有可能的字符组合?
感谢您的时间;-)
完美!感谢您的解决方案,非常感谢。 – user2109254
如果您想了解更多关于'itertools.product'实际正在做什么的内容,请阅读[笛卡尔产品](https://en.wikipedia.org/wiki/Cartesian_product)。 – pzp