简体   繁体   English

在python的2D数组中交换两个字符?

[英]Swapping two characters in a 2D array in python?

So, I'm brand new to programming, and this is frustrating me! 所以,我是编程的新手,这让我很沮丧! What I want to do is be able to import a 4x8 text file, and turn the text into a 2D list so that I can swap two characters. 我想要做的是能够导入4x8文本文件,并将文本转换为2D列表,以便我可以交换两个字符。 For example, if the imported text file looks like this: 例如,如果导入的文本文件如下所示:

OOOOOOOO
OOOXOOOO
OOOOOOOO
OOOOOOOO

then I would like to be able to change the position of the X (the row/column location) when user input is entered, such that an O will get put in its place to preserve formatting. 那么我希望能够在输入用户输入时更改X的位置(行/列位置),以便将O放在其位置以保留格式。 So, for exapmle, the program will prompt the user for their input and if the user enters "up," then the X will move one space up. 因此,例如,程序将提示用户输入,如果用户输入“ up”,则X将向上移动一个空格。

OOOXOOOO
OOOOOOOO
OOOOOOOO
OOOOOOOO

I want it to repeatedly prompt for a new move after each time one is made, and display the new grid each time (so you can see the X in its new position each time you enter a movement). 我希望它在每次完成一次动作后反复提示您进行一次新动作,并每次显示一次新的网格(这样,您每次输入动作就可以在其新位置看到X)。

This is all I have so far. 到目前为止,这就是我所拥有的。 I know I need to first find the X, but I really don't know how to. 我知道我需要先找到X,但是我真的不知道该怎么做。 I'm stuck here. 我被困在这里。 Any help is appreciated. 任何帮助表示赞赏。

#Global variables for the movements
UP = 8
DOWN = 2
RIGHT = 6
LEFT = 4

#Dimensions of grid
ROWS = 4
COLUMNS = 8

def start():        
    filename = input("Enter the name of the Start Positions file: ")
    textFile = open(filename)
    aMaze = [line.strip() for line in textFile]
    for r in range(ROWS):
        for c in range(COLUMNS):
            print(aMaze[r][c], end="")
        print()

def moveType():
    while (True):
        try:
            move = input("ENTER YOUR MOVE: ") 
        except ValueError:
            print("unimportant error message")
            continue
        if ((int(move)) in (DOWN, LEFT, RIGHT, UP)):
            playerMove(move)
            continue
        else:
            print("unimportant error message")
            continue            
    return(move)

def playerMove(move):
    move = (int(move))
    if (move == DOWN):
        #need help here
    elif (move == UP):
        #and here
    elif (move == LEFT):
        #don't know what i'm doing
    elif (move == RIGHT):
        #also here

start()
moveType()

This is a perfect opportunity to learn about abstraction. 这是学习抽象的绝好机会。 To solve your problem, think about the sub problems you could solve (with functions) that would make your final problem easier. 要解决您的问题,请考虑可以使用功能解决的子问题,这些子问题可以使最终问题更容易解决。

In your specific instance, wouldn't it be easier to write a program to find the Cartesian coordinates of where X is? 在您的特定情况下,编写程序查找X所在的笛卡尔坐标会更容易吗? With an (x,y) coordinate, you could then make a function to turn that coordinate (likely stored as a tuple) into a 2d array where that coordinate is an X an everything else is a zero. 使用(x,y)坐标,然后可以创建一个函数将该坐标(可能存储为元组)转换为2d数组,其中该坐标为X,其他所有值为零。

Hint 1: 提示1:

x =0
y =0
for row in numrows:
    for col in numcols:
        if array[row][col] == X
            y = row
            x = col

Hint 2: 提示2:

for row in numrows:
    for col in numcols:
        if col is x and row is y:
            place X
        else:
            place O

Note: if this were an application where you wanted to eek out every bit of performance, you certainly would not need to iterate through your array every time to find X. You could (and should) opt to store the location of X and then use two accesses into your array to flip X's and O's. 注意:如果这是一个您希望了解性能的每一点的应用程序,那么您当然不需要每次都遍历数组来查找X。您可以(并且应该)选择存储X的位置,然后使用两次访问数组以翻转X和O。 But seeing as this is likely one of your first problems you are solving this is of course not a concern. 但是看到这很可能是您要解决的第一个问题,这当然不是问题。

Hope this helps! 希望这可以帮助! Good luck starting to code! 祝您开始编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM