如何在Python中搜索嵌套列表网格并给出字母坐标? [英] How to search nested list grid and give lettered coordinates in Python?

查看:351
本文介绍了如何在Python中搜索嵌套列表网格并给出字母坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,为了实现这个目标而苦苦挣扎。
这是我的任务:


六字母密码是一种编码秘密消息的方法,
涉及替换和换位。加密由
开始,随机填写一个6×6网格,字母A到Z
,数字0到9(总共36个符号)。这个网格必须是消息的发送者和接收者都知道的
。网格中的行和
列标有字母A,B,C,D,E,F。

六字母密码方法。
您的程序应该:
1.创建一个6x6的网格,并用第一段中描述的字母和数字随机填充,然后提示用户输入
秘密消息。
2.在用户输入秘密消息后,显示6x6网格和生成的密文。
3.提示用户输入密文以显示原始消息。可以要求用户将
密文的每两个字母用空格或逗号隔开。


位我正在努力的是如何通过嵌套列表搜索已输入的随机放置的字母并给出坐标。也不会在数字中给出坐标,例如0,1,而不是字母,即A,B
我想我可以管理编码和解码,一旦我有了如何使用这个嵌套列表的想法。 p>

以下是我的代码:

  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]]

def main():
print(Welcome to the six cipher)
print(你想(E)编码还是(D)解码一条消息?)
operation = input()

if(operation ==E ):
encodecipher()
elif(operation ==D):
decodecipher()
else:
print(Sorry,input not recognized)
$ b $ def encodecipher():
print(请输入消息进行编码:)
messagetoencode = input()



def decodecipher():
print(Decode Test)
rowcolumn()

$ b $ def rowcolumn():
pass

main( )


解决方案

您可以使用Python的枚举来迭代值和提供每个值的索引位置:

  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, ,I,H,A]]

search ='D'

for rownum,enumerate(grid)中的行:
for colnum,枚举值(行):
if value ==搜索:
print在(%d,%d)找到的值%(rownum,colnum)


$ b

您可以将其调整为您所选择的函数结构,如下所示(如果网格中的值是唯一的):

  def findvalue(grid,value):
为rownum,枚举中的行(g rid):
为colnum,itemvalue为枚举(行):
if itemvalue == value:
return(rownum,colnum)
raise ValueError(Value not found in grid )

因为这会引发 ValueError 如果未找到该值,则必须在调用代码中处理此操作。



如果您需要将0索引的行号和列号之间的关系映射到字母A ... F你可以这样做:

  def numbertoletter(number):
if number> ; = 0和数字<= 26:
返回字符串(65 +数字)
其他:
提高ValueError('数字超出范围')
  

>>>> numbertoletter(0)
'A'
>>> numbertoletter(1)
'B'

把它放在一起给你:

  value ='B'
row,col = map(numbertoletter,findvalue(grid,value))
print 在位置(%s,%s)找到的值'%s'%(value,row,col)


I'm new to python and struggling quite a bit to get this going. This is my task:

The Six-Letter Cipher is a method of encoding a secret message that involves both substitution and transposition. The encryption starts by randomly filling a 6  6 grid with the alphabet letters from A to Z and the digits from 0 to 9 (36 symbols in total). This grid must be known to both the sender and receiver of the message. The rows and columns of the grid are labelled with the letters A, B, C, D, E, F.

Write a Python program that implements the six-letter cipher method. Your program should: 1. Create a 6x6 grid and fill it randomly with letters and numbers as described in the first paragraph, then prompt the user to enter a secret message. 2. Display the 6x6 grid and the generated ciphertext, after the user enters the secret message. 3. Prompt the user to enter the ciphertext to display the original message. It is OK to ask the user to separate every two letters of the ciphertext with a space or comma.

The bit I am struggling with is how do I search through the nested lists for the randomly placed letter that has been entered and give the coordinates. Also won't the coordinates be given in numbers i.e. 0,1, rather than letters i.e. A,B I think I could manage the encoding and decoding once I have the ideas of how to use this nested list.

Here is my code so far:

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"]]

def main():
    print("Welcome to the sixth cipher")
    print("Would you like to (E)Encode or (D)Decode a message?")
    operation = input()

    if(operation == "E"):
        encodecipher()
    elif(operation == "D"):
        decodecipher()
    else:
        print("Sorry, input not recognised")

def encodecipher():
    print("Please enter a message to encode:")
    messagetoencode = input()



def decodecipher():
    print("Decode Test")
    rowcolumn()


def rowcolumn():
    pass

main()

解决方案

You can use Python's enumerate to iterate over values and provide an index position of each value as you go:

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"]]

search = 'D'

for rownum, row in enumerate(grid):
    for colnum, value in enumerate(row):
       if value == search:
           print "Found value at (%d,%d)" % (rownum, colnum)

You can adapt this into your chosen function structure, such as the following (if values in your grid are unique):

def findvalue(grid, value):
    for rownum, row in enumerate(grid):
        for colnum, itemvalue in enumerate(row):
            if itemvalue == value:
                return (rownum, colnum)
    raise ValueError("Value not found in grid")

As this will raise a ValueError if the value isn't found, you'll have to handle this in your calling code.

If you then need to map between 0-indexed row and column numbers to the letters A...F you can do something like:

def numbertoletter(number):
    if number >= 0 and number <= 26:
        return chr(65 + number)
    else:
        raise ValueError('Number out of range')

Which will give you the following:

>>> numbertoletter(0)
'A'
>>> numbertoletter(1)
'B'

Putting it all together gives you:

value = 'B'
row, col = map(numbertoletter, findvalue(grid, value))
print "Value '%s' found at location (%s, %s)" % (value, row, col)

这篇关于如何在Python中搜索嵌套列表网格并给出字母坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆