Python Bruteforce(所有可能的组合)

问题描述 投票:0回答:3

我想从 a-zA-Z0-9 和我的最大字符串长度生成所有可能的组合。

因此,例如,如果我将最大长度设置为 25,那么我想要的输出是

a
...
z
aa
...
a1
...
zzzzzzzzzzzzzzzzzzzzzzzzz

因此,生成所有可能的组合并将每个组合打印到控制台 我是Python新手,所以我不知道如何实现这一点......

python-3.x combinations brute-force word-list
3个回答
2
投票

与例如跑步者一起运行将需要近乎永恒的时间。 max_length = 25(可能的组合数量是天文数字),但我认为这应该满足您的要求(最终):

from itertools import combinations_with_replacement

characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

max_length = 4

for r in range(1, max_length+1):
    for combo in combinations_with_replacement(characters, r=r):
        print(''.join(combo))                             

1
投票

与 akensert 的答案相同的概念,但更具可读性:

from itertools import combinations_with_replacement
from string import ascii_letters

max_length = 4

for r in range(1, max_length+1):
    for combo in combinations_with_replacement(ascii_letters, r=r):
        print(''.join(combination))   

公平警告一下,组合的数量绝对是巨大的。光是25个字符组合的数量就是:

样本大小:26+26+10 = 62

所有可能的替换组合:62^n,因此 62^25=6.25x10^44。即使您将周期时间降低到 1 纳秒,您仍然会看到 10^35 秒,即 10^27 年。这只是针对最大的集合,忽略所有 IO,并假设计算机永远不会出现故障。

如果您希望程序在宇宙终结之前完成运行,您可能需要考虑重新考虑您的方法。


0
投票

akensert的答案似乎只是将高于或等于自身的性格放在自己的右边。就像 b9 之后是 cc 而不是 ca (无法评论他的答案 bc 的低“声誉”)

© www.soinside.com 2019 - 2024. All rights reserved.