如何在Python中仅使用'O'
或'Z'
打印给定长度的所有序列?
例如,如果我提供length = 3
'OOO'
'OOZ'
'OZO'
'OZZ'
'ZOO'
'ZZO'
'ZOZ'
'ZZZ'
答案 0 :(得分:1)
您可以使用itertools
:
import itertools
letters = ["O", "Z"]
length = 3
vals = [''.join(comb) for comb in itertools.product(letters, repeat=length)]
print(vals)
>>>['OOO', 'OOZ', 'OZO', 'OZZ', 'ZOO', 'ZOZ', 'ZZO', 'ZZZ']