当前正在处理换位问题。我至今是用户输入的消息,该消息被加密成一个列表,像下面:按键排序列表
['BC', 'DE', 'DE', 'DA', 'FD', 'DD', 'BE', 'FE', 'DA', 'EA', 'FE', 'BC']
我有密码的下一阶段是把这个变成一个表的关键从用户输入。因此,如果用户输入“CODE”它输出该:
2: Enter the keyword for final encryption: code
C O D E
['B', 'C', 'D', 'E']
['D', 'E', 'D', 'A']
['F', 'D', 'D', 'D']
['B', 'E', 'F', 'E']
['D', 'A', 'E', 'A']
['F', 'E', 'B', 'C']
的下一阶段是取各列的每一个值和打印对应于其列字母的值。所以我的预期输出将是:
C D E O
['B', 'D', 'E', 'C']
['D', 'D', 'A', 'E']
['F', 'D', 'D', 'D']
['B', 'F', 'E', 'E']
['D', 'E', 'A', 'A']
['F', 'B', 'C', 'E']
我遇到的问题是试图知道如何将每个值放在其相应的列和打印它们。
这是我到目前为止有:
def encodeFinalCipher():
matrix2 = []
# Convert keyword to upper case
key = list(keyword.upper())
# Convert firstEncryption to a string
firstEncryptionString = ''.join(str(x) for x in firstEncryption)
# Print the first table that will show the firstEncryption and the keyword above it
keywordList = list(firstEncryptionString)
for x in range(0,len(keywordList),len(keyword)):
matrix2.append(list(keywordList[x:x+len(keyword)]))
# Print the un-ordered matrix to the screen
print (' %s' % ' '.join(map(str, key)))
for letters in matrix2:
print (letters)
unOrderedMatrix = [[matrix2[i][j] for i in range(len(matrix2))] for j in range(len(matrix2[0]))]
for index, item in enumerate (unOrderedMatrix):
print("\n",index, item)
index = sorted(key)
print(index)
我得到的排序键的输出:
['A', 'K', 'M', 'R']
我想知道的是怎么能这样排序的关键应用到他们代表的价值?我知道我可以通过这样做得到第一列:
print(unOrderedMatrix[0])
哪一个得到我第一列的列表。
任何帮助将不胜感激。 Python的
帮助,所以你期望为'[C,d,E,O],[B,C,d,E],[A,d,E输出, E],[D,D,D,F] ....“因为你说按字母顺序排列? – 2014-12-07 14:37:06
我试图输出对应'C'和'D'等的值。像第二个矩阵一样 – Bellos 2014-12-07 14:44:03