简体   繁体   English

在Python中打印二维列表的列

[英]Printing a column of a 2-D List in Python

Suppose if A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 假设A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Then A[0][:] prints [1, 2, 3] 然后A[0][:]打印[1, 2, 3]

But why does A[:][0] print [1, 2, 3] again ? 但为什么A[:][0] [1, 2, 3]再次打印[1, 2, 3]

It should print the column [1, 4, 7] , shouldn't it? 它应该打印[1, 4, 7] ,不应该吗?

[:] is equivalent to copy. [:]相当于复制。

A[:][0] is the first row of a copy of A. A[0][:] is a copy of the first row of A. A[:][0]A[0][:]副本的第一行。 A[0][:]是A的第一行的副本。

The two are the same. 两者是一样的。

To get the first column: [a[0] for a in A] Or use numpy and np.array(A)[:,0] 获取第一列: [a[0] for a in A]或使用numpy和np.array(A)[:,0]

如果未指定开始或结束索引,Python将返回整个数组:

A[:] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

[:] matches the entire list. [:]匹配整个列表。

So A[:] is the same as A . 所以A[:]A相同。 So A[0][:] is the same as A[0] . 因此A[0][:]A[0]相同。

And A[0][:] is the same as A[0] . A[0][:]是相同的A[0]

Note that [:] just gives you a copy of all the content of the list. 请注意, [:]只会为您提供列表中所有内容的副本。 So what you are getting is perfectly normal. 所以你得到的是完全正常的。 I think you wanted to use this operator as you would in numpy or Matlab. 我想你想在numpy或Matlab中使用这个运算符。 This does not do the same in regular Python. 这在常规Python中不会这样做。


A[0] is [1, 2, 3] A[0][1, 2, 3]

Therefore A[0][:] is also [1, 2, 3] 因此A[0][:]也是[1, 2, 3]


A[:] is [[1, 2, 3], [4, 5, 6], [7, 8, 9]] A[:][[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Therefore A[:][0] is [1, 2, 3] 因此A[:][0][1, 2, 3]


If you wanted the first column you should try: 如果你想要第一列,你应该尝试:

[e[0] for e in A]
# [1, 4, 7]

A[:] returns a copy of the entire list. A[:]返回整个列表的副本。 which is A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] A[:][0] Thus selects [1, 2, 3] . 这是A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] A[:][0]因此选择[1, 2, 3] If you want the first column, do a loop: 如果您想要第一列,请执行循环:

col = []
for row in A:
    col.append(row[0])

Problem 问题

A is not a 2-D list: it is a list of lists. A不是二维列表:它是一个列表列表。 In consideration of that: 考虑到这一点:

  1. A[0] is the first list in A: A[0]A[0]中的第一个列表:

     >>> A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> A[0] [1, 2, 3] 

    Consequently, A[0][:] : is every element of the first list: 因此, A[0][:] :是第一个列表的每个元素:

     >>> A[0][:] [1, 2, 3] 
  2. A[:] is every element of A, in other words it is a copy of A: A[:]是A的每个元素,换句话说它是A的副本:

     >>> A[:] [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 

    Consequently, A[:][0] is the first element of that copy of A. 因此, A[:][0]是该A副本的第一个元素。

     >>> A[:][0] [1, 2, 3] 

Solution

To get what you want, use numpy: 为了得到你想要的,使用numpy:

>>> import numpy as np
>>> A = np.array( [[1, 2, 3], [4, 5, 6], [7, 8, 9]] )

A is now a true two-dimensional array. A现在是一个真正的二维数组。 We can get the first row of A : 我们可以获得A的第一行:

>>> A[0,:]
array([1, 2, 3])

We can similarly get the first column of A : 我们可以类似地获得A的第一列:

>>> A[:,0]
array([1, 4, 7])

` `

A is actually a list of list, not a matrix. A实际上是列表列表,而不是矩阵。 With A[:][0] You are accessing the first element (the list [1,2,3] ) of the full slice of the list A. The [:] is Python slice notation (explained in the relevant Stack Overflow question ). 使用A[:][0]您正在访问列表A的完整切片的第一个元素(列表[1,2,3] )。 [:]是Python切片表示法(在相关的Stack Overflow问题中进行解释) )。

To get [1,4,7] you would have to use something like [sublist[0] for sublist in A] , which is a list comprehension , a vital element of the Python language. 要获得[1,4,7],你必须使用[sublist[0] for sublist in A] ,这是一个列表理解 ,是Python语言的一个重要元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM