Message='tuple' object is not an iterator Source=E:\1daer\mmxdzy\1\dzy1\dzy1.py StackTrace: File "E:\1daer\mmxdzy\1\dzy1\dzy1.py", line 45, in guess_key1 index = next(mp[i][j]) ^^^^^^^^^^^^^^ File "E:\1daer\mmxdzy\1\dzy1\dzy1.py", line 73, in <module> (Current frame) print(guess_key1(cipher_text, words)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: 'tuple' object is not an iterator
时间: 2023-07-21 19:30:56 浏览: 189
解决’tuple’ object is not callable的报错-python变量命名错误
这个错误是因为 `mp[i][j]` 不是一个迭代器,而是一个元组。在使用 `next` 函数时,需要传入一个迭代器作为参数,而不能传入一个元组。
解决这个问题的方法是将 `mp[i][j]` 转换成一个迭代器,可以使用 `iter` 函数将其转换成迭代器。修改代码如下:
```python
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)
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 = [[] for _ in range(len(mf))]
flag = True
while(flag):
key = {}
for i in range(6):
mp[i] = iter(itertools.permutations(range(len(mf[i]))))
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[i][j]` 转换成了迭代器,修改为 `iter(mp[i][j])`,然后再使用 `next` 函数来获取其下一个元素,就可以避免之前的错误。
阅读全文