2014-10-16 52 views
-2

首先我必須說我是德國人,所以對錯誤感到抱歉。這裏是我的問題:字母計數算法

我抄這個算法:

letters = list("123456") 
for current in xrange(10): 
    a = [i for i in letters] 
    for y in xrange(current): 
    a = [x+i for i in letters for x in a] 
    a = a[startpoint:] 
    for password in a: 
     print password 

從回覆此:Python Brute Force algorithm ,我想用這個算法來對一個業餘蠻力嘗試我的密碼。 我知道有在互聯網上的測試,但我想做我自己的測試。

我已經完成了這個蠻力程序,但是我發現這個算法只生成3個字母。與3個字母,它的工作是這樣的:

aa, ba, ca, da, ea, fa, usw. 

使用2個字母的所有possibilitys後,改掉這樣的:

aaa, baa, caa, daa, eaa, faa, usw. 

但如果完成了3個字母,它再次與2個字母開始了!爲什麼?

我不會真正理解這個算法做什麼,我只知道它的無限公式並被稱爲遞歸公式。要了解它完全我tryed出來的:

letters = list("123456") 
a = [x+i for i in letters for y+x in letters for y in letters] 

,但它是在其他論壇上,我得到的答覆產生「不能分配給操作」的錯誤 。基於此我試過這個:

letters = list("123456") 
a = [x+i for i in letters y+x for x in letters for y in letters] 

但它的語法錯誤。這段代碼有什麼問題?什麼意思是「不能分配給操作員」的錯誤,我給操作員分配一個值?

回答

-1

,如果你想一組使用的所有產品itertools產品

import itertools 
min_pw_len,max_pw_len = 4,5 
letters = list("123456") 
my_password = "1122" 

for my_len in range(min_pw_len,max_pw_len): 
    for guess in itertools.product(letters,repeat=my_len): 
     if "".join(guess) == my_password: 
      print "Found:",guess 

爲約語法錯誤您的實際問題

a = [x+i for i in letters y+x for x in letters for y in letters] 
          ^this is the problem ... you cannot do that (Im not entirely sure what you are trying to do) 

林guessign你想要的東西像

a2 = (x+y for x in letters for y in letters) #note that this is a generator not a list 
a3 = (x+y for x in a2 for y in letters) 
a4 = (x+y for x in a3 for y in letters) 
... 
+0

謝謝! - 我很愚蠢,我發現了一個solucion,這個itertools方法需要超過50行,所以我不看它。 和 是的,這就是我試着去做的:D謝謝 – 2014-10-16 17:38:57