2016-09-19 4 views
0

テキストファイルを、numphyや追加のプログラムを使用しないで、マトリックスとして読み込む方法を教えてください。例えば: - それは1つのカテゴリL [0]、L [1]、L [2]、Lであるかのように各列を読み取るマトリックスとしてのテキストファイル

1, 2, 3, 4 
5, 6, 7, 8 
9, 10, 11, 12 

私はそれが行列であるかのようにリストを読みたいです[3]。私は各列の平均を取るが、それに応じてテキストファイルを読む方法を知る必要がある。

ありがとうございました。

答えて

0
matrix = [] # create an empty matrix. We'll add entries to it from the file soon 
with open('path/to/file') as infile: 
    for line in infile: # for each line in the file 
     row = [float(i) for i in line.strip().split(',')] # turn each element in the line into a floating point number 
     matrix.append(row) # treat that line of floating point numbers as a list, and add it as a row of the matrix 

cols = zip(*matrix) # transpose the matrix to get the columns 
means = [sum(col)/len(col) for col in cols] # compute the mean of each column 
関連する問題