按键对列表排序

2024-09-29 21:49:38 发布

您现在位置:Python中文网/ 问答频道 /正文

目前正在研究换位问题。到目前为止,我得到的是一个用户输入一条消息,该消息被加密到一个列表中,如下所示:

 ['BC', 'DE', 'DE', 'DA', 'FD', 'DD', 'BE', 'FE', 'DA', 'EA', 'FE', 'BC']

我对密码的下一个阶段所做的是将它放入一个表中,其中包含一个从用户输入的密钥。因此,如果用户输入“代码”,它会输出:

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初学者


Tags: thetokey用户inforindexlen
3条回答
code = raw_input("Enter the keyword for final encryption:")

user_input = ['BC', 'DE', 'DE', 'DA', 'FD', 'DD', 'BE', 'FE', 'DA', 'EA', 'FE', 'BC']
user_input = ''.join(user_input)

matrix = [user_input[i:i+len(code)] for i in range(0, len(user_input), len(code))]
matrix.insert(0, code)

result = sorted([[matrix[j][ind] for j in range(len(matrix))] for ind in range(len(code)) ], key= lambda i:i[0])
for row in [[each[ind] for each in result] for ind in range(len(result[0]))]:
    print row

row结果打印为:

Enter the keyword for final encryption:CODE
['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']

下面是一些让您开始的内容(您可能希望将循环一行分隔为较小的位):

# define data
data =  [['B', 'C', 'D', 'E'], ['D', 'E', 'D', 'A'], ['F', 'D', 'D', 'D'], ['B', 'E', 'F', 'E'], ['D', 'A', 'E', 'A'], ['F', 'E', 'B', 'C']]

# choose code word
code = 'code'

# add original locations to code word [(0, c), (1, o), (2, d), (3, e))]
# and sort them alphabetically!
code_with_locations = list(sorted(enumerate(code)))

print code_with_locations # [(0, 'c'), (2, 'd'), (3, 'e'), (1, 'o')]

# re-organize data according to new indexing
for index in range(len(data)):
    # check if code is shorter than list in current index,
    # or the other way around, don't exceed either list
    max_substitutions = min(map(len, [code_with_locations, data[index]]))

    # create a new list according to new indices
    new_list = []
    for i in range(max_substitutions):
        current_index = code_with_locations[i][0]
        new_list.append(data[index][current_index])

    # replace old list with new list
    data[index] = new_list


print data

“代码”的输出为:

[['B', 'D', 'E', 'C'],
 ['D', 'D', 'A', 'E'],
 ['F', 'D', 'D', 'D'],
 ['B', 'F', 'E', 'E'],
 ['D', 'E', 'A', 'A'],
 ['F', 'B', 'C', 'E']]
msg = ['BC', 'DE', 'DE', 'DA', 'FD', 'DD', 'BE', 'FE', 'DA', 'EA', 'FE', 'BC', '12']
key = 'CODE'
# 'flatten' the message
msg = ''.join(msg)
key_length = len(key)
#create a dictionary with the letters of the key as the keys
#use a slice to create the values
columns = {k:msg[i::key_length] for i, k in enumerate(key)}
print columns
# sort the columns on the key letters
columns = sorted(columns.items())
print columns
# separate the key from the columnar data
header, data = zip(*columns)
print header
# transpose and print
for thing in zip(*data):
    print thing

>>>
{'C': 'BDFBDF1', 'E': 'EADEAC', 'D': 'DDDFEB', 'O': 'CEDEAE2'}
[('C', 'BDFBDF1'), ('D', 'DDDFEB'), ('E', 'EADEAC'), ('O', 'CEDEAE2')]
('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')
>>> 

相关问题 更多 >

    热门问题