【问题标题】:Python Bruteforce (all possible combinations)Python Bruteforce(所有可能的组合)
【发布时间】:2021-10-07 21:52:14
【问题描述】:

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

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

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

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

【问题讨论】:

  • 到目前为止你尝试过什么?我建议查看itertools 库,特别是combinations_with_replacement
  • 你可以使用https://github.com/topics/wordlist-generator?l=python来制作单词列表

标签: python-3.x combinations brute-force word-list


【解决方案1】:

它需要接近永恒才能运行,例如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(combination))                             

【讨论】:

    【解决方案2】:

    与 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,并假设计算机永远不会出现故障。

    如果您希望您的程序在世界末日之前完成运行,您可能需要考虑重新考虑您的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 2012-07-11
      • 2011-12-16
      相关资源
      最近更新 更多