查找特定图表的坐标

时间:2012-12-26 20:30:47

标签: python

我需要扫描几个字符串的列表(每行是字符串),找到符号“+”或“@”(只能出现其中一个),然后打印x,y坐标。左角是(0,0),如果你保持正确和向下,则值会增加。

我不明白我的代码有什么问题:

    x,y=-1,-1
    for row in my_list:  
            y+=1
            for symbol in row:
                    x+=1
                    if symbol=="@":
                            makom=(x,y)
                            player="@"       
                    if symbol=="+":
                            makom=(x,y)
                            player="+"


     print makom 

列表:

####
# .#
#  ###
#*@  #
#  $ #
#  ###
####

输出:

(2,3)

1 个答案:

答案 0 :(得分:2)

您在更改行时忘记重置x

y = -1
for row in my_list:  
        x = -1
        y += 1
        for symbol in row:
                x += 1
                if symbol == "@":
                        makom = (x,y)
                        player = "@"       
                if symbol == "+":
                        makom = (x,y)
                        player = "+"


print makom

为了完整起见,如果您使用enumerate:D

,就不会犯这个错误
for y, row in enumerate(my_list):
        for x, symbol in enumerate(row):
                if symbol == "@":
                        makom = (x, y)
                        player = "@"       
                if symbol == "+":
                        makom = (x, y)
                        player = "+"


print makom