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

问题描述

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

位我正在努力的是如何通过嵌套列表搜索已输入的随机放置的字母并给出坐标。也不会在数字中给出坐标,例如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和数字返回字符串(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 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,BI 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中搜索嵌套列表网格并给出字母坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:12
查看更多