在嵌套列表中定位项目并在python中解码密码

时间:2013-01-06 12:45:24

标签: python list matrix grid nested

  

可能重复:
  How to search nested list grid and give lettered coordinates in Python?

我目前有这个嵌套列表作为6x6网格:

grid = [["Z","9","G","Q","T","3"],
        ["Y","8","F","P","S","2"],
        ["X","7","E","O","R","1"],
        ["W","6","D","N","M","0"],
        ["V","5","C","L","K","U"],
        ["J","4","B","I","H","A"]]

行和列称为A,B,C,D,E,F,而不是通常的0,1,2,3,4,5。因此,需要将字母转换为数字,然后必须在嵌套列表中找到该项目。

例如,如果用户输入:FE CC ED ED CD 这应该找到HELLO的字母并打印出HELLO这个词,因为这是加密的信息。

我非常感谢您对我如何管理这方面的任何帮助。

5 个答案:

答案 0 :(得分:1)

您可以使用映射将字母转换为数字。这种映射很容易从string.ascii_uppercase字符串生成:

import string
letter_to_index = {l: i for i, l in enumerate(string.ascii_uppercase)}

使用letter_to_index然后将字母(例如E)转换为数字(4)是微不足道的:

>>> letter_to_index['E']
4

使用以空格分隔的输入,它变为:

letters = []
for coordinate in code.split():
    row, column = letter_to_index[coordinate[0]], letter_to_index[coordinate[1]]
    letters.append(grid[row][column])

print ''.join(letters)

其他技巧使用ord()获取字母的ASCII序号(减去ord('A')以使其基于0)或使用.index() string.ascii_uppercase(这将是慢)。

将它们的相对速度与timeit module进行比较是确定哪一项是最快技术的最佳方法(如果你这么小心):

>>> import timeit
>>> def lookup_map(coordinate):
...     return letter_to_index[coordinate[0]], letter_to_index[coordinate[1]]
... 
>>> def lookup_ord(coordinate, orda=ord('A')):
...     return ord(coordinate[0]) - orda, ord(coordinate[1]) - orda
... 
>>> def lookup_index(coordinate, upper=string.ascii_uppercase):
...     return upper.index(coordinate[0]), upper.index(coordinate[1])
... 
>>> coords = [random.choice(string.ascii_uppercase[:6])+random.choice(string.ascii_uppercase[:6]) for _ in range(1000)]
>>> timeit.timeit('[lookup(c) for c in coords]', 'from __main__ import coords, lookup_map as lookup', number=10000)
3.883746862411499
>>> timeit.timeit('[lookup(c) for c in coords]', 'from __main__ import coords, lookup_ord as lookup', number=10000)
4.582481861114502
>>> timeit.timeit('[lookup(c) for c in coords]', 'from __main__ import coords, lookup_index as lookup', number=10000)
9.782031059265137

字典(映射)方法最快;使用ord()紧随其后(如果您只计算ord('A')一次),使用.index()几乎需要三倍。

答案 1 :(得分:1)

def letter_to_number(letter):
    return ord(letter.upper()) - ord('A')

这个原因是ord(x)返回字符x的值,无论你的字符集是什么,无论是ASCII,Latin-1,UTF-8还是别的,所有字母都按顺序排列。

您可以通过在交互式解释器中进行试验来看到这一点:

>>> ord('A')
65
>>> ord('B')
66
>>> ord('F')
70
>>> ord('F') - ord('A')
5

答案 2 :(得分:0)

要将字符转换为整数,您可以执行以下操作:

def char_to_num(char):
    return ord(char)-ord('A')

要访问网格中的字符,您可以执行以下操作(我假设输入称为input):

word = ""
for position in input.upper():
   word += grid[char_to_num(position[0])][char_to_num(position[1])]

答案 3 :(得分:0)

>>> input_string = "FE CC ED ED CD"

# Convert input string into integers
# `ord()` converts 'A' to 65, 'B' to 66, etc
>>> pairs = [tuple(ord(letter) - ord('A') for letter in pair) for pair in input_string.split()]
[(5, 4), (2, 2), (4, 3), (4, 3), (2, 3)]

# Now just retrieve the tuples from the list of pairs
# And look up the index in the grid
>>> output = ''.join(grid[x][y] for x, y in pairs)
'HELLO'

答案 4 :(得分:0)

index=input('Enter the index:')
index=list(index)
a=[ord(i)-ord('A') for i in index]
print(grid[a[0]][a[1]])