mp = [[iter(range(len(mf[i]))) for j in range(len(mf[i]))] for i in range(len(mf))] for i in range(len(mf)): for j in range(len(mf[i])): mp[i].append(None) flag = True while(flag): key = {} for i in range(6): mp[i] = itertools.permutations(range(len(mf[i]))) flag=False这段代码的问题?
时间: 2024-04-25 21:26:30 浏览: 178
这段代码有几个问题:
1. 缺少对`itertools`模块的导入
2. 在第3行中,`mf`列表没有被定义,可能会导致`NameError`异常。
3. 在第5行中,将`None`添加到了`mp[i]`的末尾,但是`mp[i]`被定义为一个由迭代器组成的列表,因此这个操作可能会导致混淆和错误。
4. 在第8行中,`itertools.permutations()`返回的是一个迭代器对象,但是它没有被保存到任何变量中,因此这个操作实际上是没有意义的。
5. 在第10行中,`flag`被设置为`False`,但是在之前的代码中并没有修改它的值,因此这个循环可能会立即终止。
另外,这段代码中出现了硬编码的常量`6`,不够通用化,应该使用`len(mf)`来代替。
相关问题
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))] for i in range(len(mf)): for j in range(len(mf[i])): mp[i].append(None) flag = True while(flag): key = {} for i in range(len(mf)): mp[i] = itertools.permutations(range(len(mf[i])))我这里得到的mp会是?
根据代码可以看出,mp是一个二维列表,包含了6个子列表,每个子列表都是由若干个迭代器和None元素构成的。迭代器用来生成每个子列表中的排列。根据代码中的实现,mp的形状应该是一个6行12列的二维列表,其中前6列是迭代器,后6列是None元素。具体的mp列表如下所示:
```python
mp = [
[iter(range(len(f1))), None, None, None, None, None, None, None, None, None, None, None],
[iter(range(len(f2))), iter(range(len(f2))), iter(range(len(f2))), iter(range(len(f2))), iter(range(len(f2))), iter(range(len(f2))), None, None, None, None, None, None],
[iter(range(len(f3))), iter(range(len(f3))), iter(range(len(f3))), None, None, None, None, None, None, None, None, None],
[iter(range(len(f4))), iter(range(len(f4))), iter(range(len(f4))), iter(range(len(f4))), iter(range(len(f4))), None, None, None, None, None, None, None],
[iter(range(len(f5))), iter(range(len(f5))), iter(range(len(f5))), iter(range(len(f5))), iter(range(len(f5))), None, None, None, None, None, None, None],
[iter(range(len(f6))), iter(range(len(f6))), iter(range(len(f6))), iter(range(len(f6))), iter(range(len(f6))), iter(range(len(f6))), None, None, None, None, None, None]
]
```
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))] for i in range(len(mf)): for j in range(len(mf[i])): mp[i].append(None) flag = True while(flag): key = {} for i in range(len(mf)): mp[i] = itertools.permutations(range(len(mf[i]))) flag=False 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 我的想法是,每次取mp的一种排列,得到一个key,检验不通过再取mp的下一种排列,如何修改?
你可以在`while`循环内部使用一个`for`循环来遍历`mp`中的所有排列,如果某个排列解密后能通过检验,则直接返回`key`,否则继续遍历下一个排列。修改后的代码如下:
```
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)
most_common_letters_m = [sorted_letters[:8], sorted_letters[8:16], sorted_letters[-10:]]
f1 = ['e']
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']
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))]
for i in range(len(mf)):
for j in range(len(mf[i])):
mp[i].append(None)
flag = True
while(flag):
for i in range(len(mf)):
for j in range(len(mf[i])):
index = next(mp[i][j])
key = {}
for k in range(len(most_common_letters_m)):
key[most_common_letters_m[k][index]] = mf[k][j]
decrypted_text = decrypt(cipher_text, key)
if is_plaintext(decrypted_text, words):
return key
flag = False
```
阅读全文