只使用给定大小的组合制作野兽

  • 本文关键字:组合 野兽 python
  • 更新时间 :
  • 英文 :


也许标题不清楚,让我自己解释一下。

假设我有一个字符串:"1234567890";

我想用一种特殊性来对待它:每个组合都必须有6个字符长,不能少,不能多。我已经得到了一个itertools的完整bruteforce(包括字母表、数字和符号(的例子,它给了我所有长度从1到6的值,但我不想这样。。。

这里是btw:

from itertools import chain, product
def bruteforce(charset, maxlength):
file = open("test.txt", "w")
return (''.join(candidate)
for candidate in chain.from_iterable(product(charset, repeat=i)
for i in range(1, maxlength + 1)))
var = str((list(bruteforce("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ²&é(-è_çà)=^$*ù,;:!<1234567890°+¨£µ%?./§>~{[|`^@]}¤€ .", 6))))
file.write(var)
file.close()
print("Done")

那么你知道怎么做吗,也许是用numpy?我已经想过做一个";经典的";brueforce,然后使用for循环,删除len(element(<6,但这需要很多时间,而且它真的没有优化。

我也看到了:如何使用numpy获得所有可能的洗牌组合但它似乎只需要2个数字,0或1,而不是我想要的10个数字。我真的不了解numpy,所以也许有办法适应它?

itertools.combinations_with_replacement(iterable,r(就是为了这个:

从输入迭代中返回r长度的元素子序列从而允许单个元素重复不止一次。

from itertools import combinations_with_replacement
chars = "1234567890"
for candidate in combinations_with_replacement(chars, r=6):
print(''.join(candidate))

输出:

111111
111112
111113
111114
111115
111116
111117
111118
111119
111110
111122
111123
111124
111125
...

如果我理解你的问题,你需要itertools.termutations或itertools.combinations

from itertools import permutations #or combinations
def bruteforce(charset, maxlength):
return (''.join(candidate) for candidate in permutations(charset, maxlength))
#or return (''.join(candidate) for candidate in combinations(charset, maxlength))

最新更新