Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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_Python 3.x - Fatal编程技术网

Python 从命令行读取输入将其存储到二维数组中,而不是字符串输入

Python 从命令行读取输入将其存储到二维数组中,而不是字符串输入,python,python-3.x,Python,Python 3.x,使用此函数,当我打印阵列时,我将以下内容作为输出: with open(sys.argv[1], 'r') as f: a= f.readlines() #reads the file's lines a = [x.strip() for x in a] #ignores the newline char print (a) 如何转换上述函数以使其返回 ['+X..XX....-', '.X..X..X-..', '.X........

使用此函数,当我打印阵列时,我将以下内容作为输出:

with open(sys.argv[1], 'r') as f:                  
    a= f.readlines()    #reads the file's lines
    a = [x.strip() for x in a] #ignores the newline char
print (a)
如何转换上述函数以使其返回

['+X..XX....-', '.X..X..X-..', '.X.........', '...XX......', 'XXX.+X.....', '..X.....XXX', '...XXX..X-.', '.-.....X...']

类似于这样,一个元素数组而不是字符串数组

请共享真实文件的4行。格式化为代码。现在还不清楚你的文件中是否有很多X,是否只包含0,或者数字的格式是什么,也不清楚这些数字是如何相互分离的。我不完全清楚你当前的输出和你想要的输出之间的关系。请分享你真实文件的4行。格式化为代码。现在还不清楚你的文件中是否有很多X,是否只包含0,或者数字的格式是什么,也不清楚这些数字是如何相互分离的。我不完全清楚你当前的输出和你想要的输出之间的关系。
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
In[3]: with open('test.txt', 'r') as f:
  ...:     result = [list(line.rstrip()) for line in f]
  ...: 
In[4]: result
Out[4]: 
[['+', 'X', '.', '.', 'X', 'X', '.', '.', '.', '.', '-'],
 ['.', 'X', '.', '.', 'X', '.', '.', 'X', '-', '.', '.'],
 ['.', 'X', '.', '.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', 'X', 'X', '.', '.', '.', '.', '.', '.'],
 ['X', 'X', 'X', '.', '+', 'X', '.', '.', '.', '.', '.'],
 ['.', '.', 'X', '.', '.', '.', '.', '.', 'X', 'X', 'X'],
 ['.', '.', '.', 'X', 'X', 'X', '.', '.', 'X', '-', '.'],
 ['.', '-', '.', '.', '.', '.', '.', 'X', '.', '.', '.']]