我要使most_common_letters_m的分割如f1-f6而不是修改f1-f6
时间: 2024-03-14 17:47:18 浏览: 40
好的,如果你要按照f1-f6的方式分割`most_common_letters_m`,可以按照下面的方式修改代码:
```
f1 = ['e'] # >10%
f2 = ['a', 'i', 'r', 't', 'o', 'n'] # high 8
f3 = sorted_letters[8:14] # middle-upper 6
f4 = sorted_letters[14:20] # middle-lower 6
f5 = sorted_letters[20:26] # low 6
f6 = sorted_letters[26:] # lowest 6
most_common_letters_m = [f1, f2, f3, f4, f5, f6]
```
这样,`most_common_letters_m`就被分成了6个部分,分别对应f1-f6的6个部分。其中,`f1`表示频率最高的前1个字母,频率大于10%;`f2`表示频率次高的6个字母;`f3`表示频率居中偏上的6个字母;`f4`表示频率居中偏下的6个字母;`f5`表示频率最低的6个字母;`f6`表示频率最低的6个字母。
相关问题
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'] 修改这段代码 使most_common_letters_m分成f1-f6形式的6段
好的,你可以按照下面的方式修改代码,将`most_common_letters_m`分成6个部分:
```
f1 = sorted_letters[:8] # >10%
f2 = sorted_letters[8:16] # high 8
f3 = sorted_letters[16:22] # middle-upper 6
f4 = sorted_letters[22:27] # middle-lower 5
f5 = sorted_letters[27:32] # low 5
f6 = sorted_letters[-6:] # lowest 6
most_common_letters_m = [f1, f2, f3, f4, f5, f6]
```
这样,`most_common_letters_m`就被分成了6个部分,分别对应6个频率段。其中,`f1`表示频率最高的前8个字母,频率大于10%;`f2`表示频率次高的8个字母;`f3`表示频率居中偏上的6个字母;`f4`表示频率居中偏下的5个字母;`f5`表示频率最低的5个字母;`f6`表示频率最低的6个字母。
def guess_key3(cipher_text, word1, word2, word3): #变了点 letter_frequency = get_letter_frequency(cipher_text.lower()) excluded_letters = [letter for letter in letter_frequency.keys() if letter_frequency[letter] == 0] sorted_letters = sorted([letter for letter in letter_frequency.keys() if letter_frequency[letter] > 0], key=lambda x: letter_frequency[x], reverse=True) print("Excluded letters:", excluded_letters) print() f1 = ['a', 'i', 'r'] f2 = ['t', 'o', 'n'] f3 = ['s', 'l', 'c'] f4 = ['u', 'p', 'm', 'd', 'h'] f5 = ['g', 'b', 'y', 'f'] f6 = ['v', 'w','k'] f7 = ['x', 'z', 'q', 'j'] mf = [f6, f5, f4, f3, f2, f1] key = {sorted_letters[0]: 'e'} most_common_letters_m = [sorted_letters[19:22],sorted_letters[15:19] ,sorted_letters[10:15] ,sorted_letters[7:10] ,sorted_letters[4:7], sorted_letters[1:4]] c1 = 0.7 for i1 in range(5): lk1=len(key) key1 =check3(cipher_text, word1, word2, word3, most_common_letters_m, sorted_letters, mf, f7, lk1, key, c1, i1) key.update(key1) del mf[-1] del most_common_letters_m[-1] c1+=0.2 return key def check3(cipher_text, word1, word2, word3, most_common_letters_m, sorted_letters, mf, f7, key, lk1, c1, i1): mp = [[j for j in range(len(mf[i]))] for i in range(len(mf))] row_permutations = [itertools.permutations(row) for row in mp] matrix_permutations = itertools.product(*row_permutations) for permutation in matrix_permutations: for i in range(len(mp)): for j in range(len(mp[i])): key[most_common_letters_m[i][permutation[i][j]]] = mf[i][j] if len(key) < len (sorted_letters): for i in range (len(sorted_letters)-len(key)): key.update({sorted_letters[len(key)+i-1]:f7[i]}) decrypted_text = decrypt(cipher_text, key) k1 = is_plaintext3(decrypted_text, word1, word2 ,word3) #k2 = k1 if k1 > k2 else k2 if( k1 > c1): key1 = dict(list(key())[lk1:len(mp[i])+lk1]) return key1 def is_plaintext3(text, word1, word2 ,word3): words_found = 0 for word in text.split(): if word.lower() in word1: words_found += 10 if word.lower() in word2: words_found += 3 if word.lower() in word3: words_found += 1 return words_found / len(text.split()) def decrypt(cipher_text, key): mapping_dict = str.maketrans(key) return cipher_text.translate(mapping_dict)这段代码的问题
这段代码中的 `check3` 函数在定义时参数列表中包含了六个参数,但在调用时只传递了九个参数,因此会导致参数数量不匹配的错误。具体来说,`check3` 函数在调用时缺少了 `lk1`、`key`、`c1`、`i1` 这四个参数。你需要检查一下代码逻辑,确认这四个参数的值应该从哪里传递进去,并在调用时将它们添加到参数列表中。
阅读全文