extra a column from a 2-D array?

Robert Kern rkern at ucsd.edu
Tue May 10 04:08:47 EDT 2005


Joe Wong wrote:
> Hello,
>  
>  Suppose I have a python array as follow:
>  
>     s=[ [1,3,5],
>           [2,4,6],
>           [9,8,7]]
>  
> what is the quickest way to extract the second colum from all rows? That 
> is: [3,4,8] in this example.

Use Numeric. http://numeric.scipy.org

from Numeric import *
s = array([[1,3,5], [2,4,6], [9,8,7]])
print s[:,1]

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter




More information about the Python-list mailing list