Python 3.x 在python中通过用户输入选择数组的坐标

Python 3.x 在python中通过用户输入选择数组的坐标,python-3.x,Python 3.x,这是一个魔术方块,列表在记事本文本文件中。如何选择列表中的行和列,以便更改列表中特定坐标的值 例如,用户输入坐标[0][1]。如何在记事本文件中找到该坐标,并提示用户输入一个整数以替换坐标[0][1]处的原始值?假设您的输入文件sample.txt有一个由空格分隔的数字列表'包含以下内容: file_name = input("Enter the file name: ") #magicsquares.txt #show users the table infile = open(file_na

这是一个魔术方块,列表在记事本文本文件中。如何选择列表中的行和列,以便更改列表中特定坐标的值


例如,用户输入坐标[0][1]。如何在记事本文件中找到该坐标,并提示用户输入一个整数以替换坐标[0][1]处的原始值?

假设您的输入文件
sample.txt
有一个由空格分隔的数字列表
'
包含以下内容:

file_name = input("Enter the file name: ") #magicsquares.txt
#show users the table
infile = open(file_name, "r")
file = infile.readlines()
infile.close()
#converting the numbers in notepad into a list (integers)
table= []
for i in range (len(file)):
    row= file[i].split(' ')
for j in range (len(row)):
    row[j]= int(row[j])
table.append(row)
print (row)


#selecting cells
select1 = int(input("Enter row number: "))
select2= int(input("Enter column number: "))
以上是一个矩阵式结构

python代码如下所示:

1 2 3 4
5 6 7 8
9 0 1 2
1 4 7 9
import fileinput
import linecache

file_name = input("Enter the file name: ") #sample.txt

# selecting cells
select1 = int(input("Enter row number: ")) #0
select2 = int(input("Enter column number: ")) #1

# New value
newVal = int(input("Enter row number: ")) #100

# Get nth line from the input file and strip any newline chars
textToSearch = linecache.getline(file_name, select1+1).strip('\n')

# Transform to the line read to a list 
tmpList = [int(_) for _ in textToSearch.split(' ')]

# Replace the list with the new value and form the str line back again
tmpList[select2] =  newVal
textToReplace = ' '.join(str(_) for _ in tmpList)

# Modify the sample.txt file inplace 
with fileinput.FileInput(file_name, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(textToSearch, textToReplace), end='')

file.close()
linecache.clearcache()
sample.txt
现在应该如下所示:

1 2 3 4
5 6 7 8
9 0 1 2
1 4 7 9
import fileinput
import linecache

file_name = input("Enter the file name: ") #sample.txt

# selecting cells
select1 = int(input("Enter row number: ")) #0
select2 = int(input("Enter column number: ")) #1

# New value
newVal = int(input("Enter row number: ")) #100

# Get nth line from the input file and strip any newline chars
textToSearch = linecache.getline(file_name, select1+1).strip('\n')

# Transform to the line read to a list 
tmpList = [int(_) for _ in textToSearch.split(' ')]

# Replace the list with the new value and form the str line back again
tmpList[select2] =  newVal
textToReplace = ' '.join(str(_) for _ in tmpList)

# Modify the sample.txt file inplace 
with fileinput.FileInput(file_name, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(textToSearch, textToReplace), end='')

file.close()
linecache.clearcache()
注意:这是Python3特有的

重要资源:

  • 其他值得一读的SO QA:


  • 问题是我似乎无法选择坐标,并要求用户在数组中的特定坐标中输入一个新的整数值;您的代码似乎在您尝试之前就停止了。我认为您需要嵌套第二个for循环,以便正确生成表,然后我认为您只需执行
    table[select1][select2]=…
    我完全同意。。。。我们计划先从单元格选择开始构建代码,然后只插入循环,以确保一切一步一步地工作:)谢谢!