Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.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_Matrix - Fatal编程技术网

Python 从矩阵打印某些位置

Python 从矩阵打印某些位置,python,matrix,Python,Matrix,如果我的矩阵像[[1,2,3,4],[5,3,6,2],[7,7,2],[9,7,5,3] 我想打印第二行的位置,这样输出就会像这样: [1][0] [1][1] [1][2] [1][3] 我想我需要为循环使用,但不确定具体如何使用 thnx尝试使用此 matrix = [[1,2,3,4],[5,3,6,2],[7,7,7,2],[9,7,5,3]] [print(x) for x in matrix[1]] 或 或 三者之间的区别只是格式不同。第二个将按如下方式打印矩阵[1]中的值:[

如果我的矩阵像
[[1,2,3,4],[5,3,6,2],[7,7,2],[9,7,5,3]

我想打印第二行的位置,这样输出就会像这样:

[1][0]
[1][1]
[1][2]
[1][3]
我想我需要为循环使用
,但不确定具体如何使用

thnx

尝试使用此

matrix = [[1,2,3,4],[5,3,6,2],[7,7,7,2],[9,7,5,3]]
[print(x) for x in matrix[1]]

三者之间的区别只是格式不同。第二个将按如下方式打印
矩阵[1]
中的值:
[5,3,6,2]
第一个将按如下方式打印

5
3
6
2
第三个是这样的

5 from [1][0]
3 from [1][1]
6 from [1][2]
2 from [1][3]

希望这就是您想要的

Python为几乎所有目的提供内置功能。使用
枚举
函数和字符串格式:

for i, j in enumerate(matrix[1]):
    print '[1][{}] contains {}'.format(i, j)

你真的想打印你作为例子放的东西,还是想打印与这些索引相关联的值?那么你尝试了什么?考虑一个for循环。枚举是走的路!@你做错了什么。“那不管用!”马利克布拉希米当我跑的时候,我觉得一切都很好。(如果你的意思是捕捉错误-例如,indexerror,我故意不放它)我现在明白了。您正在为每一行使用每个方法。
5 from [1][0]
3 from [1][1]
6 from [1][2]
2 from [1][3]
for i, j in enumerate(matrix[1]):
    print '[1][{}] contains {}'.format(i, j)