在Python中打印二维列表的列 [英] Printing a column of a 2-D List in Python
本文介绍了在Python中打印二维列表的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
然后A[0][:]
打印[1, 2, 3]
但是为什么A[:][0]
再次打印[1, 2, 3]
?
But why does A[:][0]
print [1, 2, 3]
again ?
它应该打印列[1, 4, 7]
,不是吗?
It should print the column [1, 4, 7]
, shouldn't it?
推荐答案
[:]
等同于复制.
A[:][0]
是A副本的第一行.
A[0][:]
是A的第一行的副本.
A[:][0]
is the first row of a copy of A.
A[0][:]
is a copy of the first row of A.
两者是相同的.
要获取第一列:[a[0] for a in A]
或使用numpy和np.array(A)[:,0]
To get the first column: [a[0] for a in A]
Or use numpy and np.array(A)[:,0]
这篇关于在Python中打印二维列表的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文