我正在使用itertools生成组合,但是我想控制输出并能够要求下一个输入:
from itertools import permutations
def getPass():
chars = '4$5%6^7&'
passd = ''
for comb in permutations(chars):
passd = ''.join(comb)
return passd
while(True):
print getPass()
但是找不到任何相关文档.我该怎么做呢?
解决方法:
您可以使用generator:
from itertools import permutations
def getPass():
chars = '4$5%6^7&'
for comb in permutations(chars):
yield ''.join(comb)
for i in getPass():
print(i)
input('\ncontinue?')
from itertools import permutations
def getPass():
return (''.join(p) for p in permutations('4$5%6^7&'))
for i in getPass():
print(i)
input('\ncontinue?')
输出:
4$5%6^7& continue? 4$5%6^&7 continue? 4$5%67^& continue? 4$5%67&^ continue? 4$5%6&^7 continue? ...