4

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

但我找不到任何相关文件。我该怎么做呢?

4

1 回答 1

3

您可以使用生成器

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&

继续?
4$5%6^&7

继续?
4$5%67^&

继续?
4$5%67&^

继续?
4$5%6&^7

继续?
...
于 2013-06-07T12:01:51.383 回答