# 读取.mat文件
mat_data = scipy.io.loadmat('C.mat')
# 获取矩阵数据
adj_matrix = mat_data['C']
一个简短的python代码:
alph=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] A0=[.082,.015,.028,.043,.127,.022,.020,.061,.070,.0015,.0077,.040,.024,.067,.075,.019,0.001,.060,.063,.091,.028,.0098,.024,.0015,.020,.00074] #Takes the dot product of two vectors. The inputs V1 and V2 should be list objects. def DotProduct (V1 : list, V2 : list): assert len(V1)==len(V2) dp=0 for i in range(len(V1)): dp+=V1[i]*V2[i] return dp # Takes a vector and shifts its elements by a given amount. #Elements at the end wrap around to the front of the new vector. def VectorShift( V : list, shift : int): S=[] for i in range(len(V)): index = (i+shift)%len(V) S.append(V[index]) return S def FrequencyCount( Cipher : str): count = [0] * len(alph) for letter in Cipher: if letter in alph: index = alph.index(letter) count[index] += 1 return count #DotProduct(alph,A0) a=[1,2,3,4,5] S=VectorShift(a,2) print(S) str='AACCCDZZ'; SS=FrequencyCount(str) print(SS) def ShiftCipher(cipher): freq = FrequencyCount(cipher) # 统计字母频率 max_freq_index = freq.index(max(freq)) # 找到出现频率最高的字母索引 shift = (max_freq_index - alph.index('E')) % len(alph) # 计算需要进行的字母位移 plaintext = '' for letter in cipher: if letter in alph: index = (alph.index(letter) - shift) % len(alph) # 进行字母位移解密 plaintext += alph[index] else: plaintext += letter # 非字母字符保持不变 return plaintext cipher= 'ABCDE' sss=ShiftCipher(cipher) print(sss)
标签:index,python,代码,len,V1,letter,freq,alph From: https://www.cnblogs.com/Hello-Vivi/p/17987439