我正在使用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&
继续?
4美元5%6^&7
继续?
4美元5%67^&
继续?
4美元5%67&^
继续?
4$5%6和^7
继续?
...

关于python - Python itertools组合迭代控制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16983621/

10-09 20:20