Python itertools组合迭代控制

我正在使用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?')

甚至是generator expression

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?
...
上一篇:如何在Python中的静态,可执行公式上遍历算术运算符?


下一篇:python-TypeError:’Figure’对象不可迭代(itertools)