for p in itertools.product(*(itertools.permutations(range(len(mf[i]))) for i in range(6))): for i in range(6): for j in range(len(mf[i])): print(p[i][j], end=" ") break将这段代码改写为,使mp为mf的排列
时间: 2024-02-14 09:11:39 浏览: 138
Python itertools.product方法代码实例
可以将for循环的第一行修改为以下代码:
```
for p in itertools.product(*[itertools.permutations(mf[i]) for i in range(6)]):
```
这样就能将mp改为mf的排列了。修改后的完整代码如下:
```
def guess_key1(cipher_text, words):
letter_frequency = get_letter_frequency(cipher_text.lower())
sorted_letters = sorted(letter_frequency.keys(), key=lambda x: letter_frequency[x], reverse=True) # paixu
most_common_letters_m = [sorted_letters[:8], sorted_letters[8:16], sorted_letters[-10:]] # high 8 middle 8 low 10
f1 = ['e'] # >10%
f2 = ['a', 'i', 'r', 't', 'o', 'n']
f3 = ['s', 'l', 'c']
f4 = ['u', 'p', 'm', 'd', 'h']
f5 = ['g', 'b', 'y', 'f', 'v']
f6 = ['w', 'k', 'x', 'z', 'q', 'j'] # <1%
mf = [f1, f2, f3, f4, f5, f6]
mp = [[iter(range(len(mf[i]))) for j in range(len(mf[i]))] for i in range(len(mf))]
flag = True
while(flag):
key = {}
for p in itertools.product(*[itertools.permutations(mf[i]) for i in range(6)]):
for i in range(6):
for j in range(len(mf[i])):
print(p[i][j], end=" ")
break
for i in range(6):
for j in range(len(mf[i])):
index = next(mp[i][j])
key.update({most_common_letters_m[i][index]: mf[i][j]})
decrypted_text = decrypt(cipher_text, key)
if is_plaintext(decrypted_text, words):
flag=False
return key
```
阅读全文