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

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

问题描述

我是 Python 的新手,为了实现这个目标付出了很多努力.这是我的任务:

<块引用>

六字母密码是一种对秘密信息进行编码的方法包括置换和转座.加密开始于用 A 到 Z 的字母随机填充 6×6 方格以及从 0 到 9 的数字(共 36 个符号).这个网格必须是消息的发送者和接收者都知道.行和网格的列标有字母 A、B、C、D、E、F.

编写一个实现六字母密码方法的 Python 程序.你的程序应该:1. 创建一个 6x6 的网格,并按照第一段所述用字母和数字随机填充,然后提示用户输入一个秘密信息.2. 用户输入密文后,显示 6x6 网格和生成的密文.3. 提示用户输入密文显示原始消息.可以要求用户将每两个字母分开带有空格或逗号的密文.

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

这是我目前的代码:

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"]]定义主():print("欢迎使用第六个密码")打印(你想(E)编码或(D)解码消息?")操作 = 输入()如果(操作==E"):编码密码()elif(操作==D"):解码密码()别的:打印(对不起,输入无法识别")定义编码密码():print("请输入要编码的信息:")消息编码 = 输入()定义解码密码():打印(解码测试")行列()定义行列():经过主要的()

解决方案

您可以使用 Python 的 enumerate 来迭代值并随时提供每个值的索引位置:

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"]]搜索 = 'D'对于 rownum,枚举(网格)中的行:对于列,枚举(行)中的值:如果值 == 搜索:打印在 (%d,%d) 处找到的值"% (rownum, colnum)

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

def findvalue(grid, value):对于 rownum,枚举(网格)中的行:对于列,枚举(行)中的项目值:如果项目值 == 值:返回(行数,列数)raise ValueError("在网格中找不到值")

因为如果找不到该值,这将引发 ValueError,所以您必须在调用代码中处理此问题.

如果您随后需要将 0 索引的行号和列号映射到字母 A...F,您可以执行以下操作:

def numbertoletter(number):如果 number >= 0 并且 number <= 26:返回 chr(65 + 数字)别的:raise ValueError('数字超出范围')

这将为您提供以下信息:

<预><代码>>>>数字字母(0)'一个'>>>数字字母(1)'乙'

把它们放在一起给你:

value = 'B'row, col = map(numbertoletter, findvalue(grid, value))打印 "在位置 (%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天全站免登陆