0

I have a list which contains sublists of three strings:

Example:

data=[
      [' 2.131e+02', ' 2.186e+02', '-9.073e+00'],
      [' 6.205e+01', ' 6.633e+01', '-1.708e+01'],
      ['-4.220e-01', '-7.210e-02', '-5.062e-01']
     ]

Ho can I select a column?

I can select data[0] which would be the first row, but I am not able to select a column. I thought the following would select the elements of the first column data[:][0] but it selects again the first row?

len
  • 749
  • 1
  • 8
  • 23

2 Answers2

1

you can achieve what you want with:

[x[0] for x in data]
1
data=[
      [' 2.131e+02', ' 2.186e+02', '-9.073e+00'],
      [' 6.205e+01', ' 6.633e+01', '-1.708e+01'],
      ['-4.220e-01', '-7.210e-02', '-5.062e-01']
     ]

def get_column(matrix, i):
    return [row[i] for row in matrix]
    
print(get_column(data, 1))

i is the index of the column that you want to get.

Aymen
  • 841
  • 1
  • 12
  • 21