我需要编写生成所有可能的8个符号字符串的生成器。 从这样的符号数组中:
leters = ['1','2','3','4','5','6','7','8','9','0','q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m']
骨架看起来是这样的:
def generator():
"""
here algorithm
"""
yield string
假设返回这样的列表['00000001','00000002','00000003', ......'mmmmmmmm']
itertools.combinations()
和itertools.combinations_with_replacement()
返回生成器我在示例中使用
print()
来演示输出。用yield
代替它,得到一个生成器。如果对包含英文字母和数字的所有8个字母的密码都强制使用它,则需要迭代大约2.8万亿个字符串
编辑 如果您知道没有重复的元素,可以使用
permutations
这将同时为您提供ab和ba
对于最一般的蛮力序列,使用宇宙学解中的
itertools.product()
编辑:让它给你字符串而不是元组:
顺便说一下,字母有两个T
相关问题 更多 >
编程相关推荐