Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将文件中的前几行元素转换为二维数组_Python_Arrays_List - Fatal编程技术网

Python 如何将文件中的前几行元素转换为二维数组

Python 如何将文件中的前几行元素转换为二维数组,python,arrays,list,Python,Arrays,List,所以我要做的就是从数据文件中创建一个3x3表。例如,如果给数据文件提供了一些 0 5 2 7 8 4 1 6 3 4 U 2 D 2 L 1 D 2 我需要一张3x3的桌子 0 5 2 7 8 4 1 6 3 我

所以我要做的就是从数据文件中创建一个3x3表。例如,如果给数据文件提供了一些

0 5 2
7 8 4
1 6 3
4
U 2
D 2
L 1
D 2
我需要一张3x3的桌子

                                               0 5 2 
                                               7 8 4
                                               1 6 3
我试过我的方法

def readfile(x):
    torusSquare=[]
    file= open(x)
    count=0
    while count<3:
        line = file.readline()
        line=line.rstrip('\n').split(' ')
        vals=[]
        for val in line:
            vals.append(int(val))
        torusSquare.append(vals)
        count+=1
但是当我尝试调用另一个函数时,例如
printArray(toursquare)
下面是函数

def printArray(torusSquare):
    for i in range(3):
        for j in range(3):
            print(torusSquare[i][j],' ',end=' ')
        print()
我犯了一个错误

    print(torusSquare[i][j],' ',end=' ')
IndexError: list index out of range

如果我像这样运行您的代码:

def readfile(x):
    torusSquare=[]
    file= open(x)
    count=0
    while count<3:
        line = file.readline()
        line = line.rstrip('\n').split(' ')
        vals=[]
        for val in line:
            vals.append(int(val))
        torusSquare.append(vals)
        count+=1

    if count==3:
        for i in range(count):
            for j in range(count):
                print(torusSquare[i][j],' ',end=' ')
        print()

    return torusSquare

def printArray(torusSquare):
    for i in range(3):
        for j in range(3):
            print(torusSquare[i][j],' ',end=' ')
        print()


if __name__ == '__main__':

    ts = readfile('data.txt')

    printArray(ts)
在函数readfile(x)中,我添加了以下行:

return torusSquare
这使得函数返回torusSquare列表(因此,在您的torusSquare示例中,当您调用函数时,它会“吐出”返回值)。然后,您可以将其分配给任意变量名(如“ts”),如下所示:

ts = readfile('data.txt')
然后,您可以将其发送到printArray函数。

使用熊猫:

import pandas as pd

df = pd.read_table('your_file.txt', sep= ' ',header=None, nrows=3)
array = df.to_numpy().astype(int)

print(array)
#
[[0 5 2]
 [7 8 4]
 [1 6 3]]

用你自己的话来说,为什么你希望你在
readfile
中创建的
torusSquare
printary
接收的
torusSquare
作为参数有关?此外,如果您从尝试读取文件的代码中获得了正确的输出,那么您的问题显然不是如何读取文件,而是代码中的其他地方。请询问您实际遇到的问题。另外,请阅读并帮助我们帮助您。函数printary基本上假设将[[0,5,2],[7,8,4],[1,6,3]]转换为实际数组,即3x3表。我们在Python中没有内置数组;我们有列表-您的
[[0,5,2],[7,8,4],[1,6,3]。
结果已经是这样了,您可以将其作为3x3表格使用。您在
printary
代码中显示的所有内容都可以使用您计算的
torusSquare
值来完成。问题是您的代码实际上并没有导致
printary
使用该值。您可能需要查看函数调用是如何工作的,返回的
是什么意思以及它是如何工作的,等等。@moonahmad您是否从
readfile
返回
torusSquare
?但是如果您实际显示了代码的相关部分,我只能告诉您代码其余部分的错误。再次,请阅读我给你的链接,以便理解如何正确地提问。在计数+=1之后,我添加了if count==3:for I in range(计数):for j in range(计数):print(torusSquare[i] [j],“”,end=“”)print()似乎也起作用了。我更新了我的答案。也许意图是错误的?尽管这没有意义,只是为了调试。
ts = readfile('data.txt')
import pandas as pd

df = pd.read_table('your_file.txt', sep= ' ',header=None, nrows=3)
array = df.to_numpy().astype(int)

print(array)
#
[[0 5 2]
 [7 8 4]
 [1 6 3]]