我正在使用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()
但是找不到任何相关文档.我该怎么做呢?
解决方法:
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?
...
标签:iteration,combinations,itertools,python
来源: https://codeday.me/bug/20191123/2065697.html