陣列我在蟒使用numpy的讀取csv文件:使用迭代來總結在python
import numpy as np
import csv
from StringIO import StringIO
with open ('1250_12.csv','rb') as csvfile:
data = np.genfromtxt(csvfile, dtype = None, delimiter = ',')
np.set_printoptions(threshold='nan'
打印出如下:
[['x1' 'y1' 'z1' 'x2' 'y2' 'z2' 'cost']
['5720.44' '3070.94' '2642.19' '5797.82' '3061.01' '2576.29' '102.12']
['5720.44' '3070.94' '2642.19' '5809.75' '3023.6' '2597.81' '110.4']
['5861.54' '3029.08' '2742.36' '5981.23' '3021.52' '2720.47' '121.92']
['5861.54' '3029.08' '2742.36' '5955.36' '3012.95' '2686.28' '110.49']
所以第一列屬於「X1 ',第二列屬於'x2'等。可以說x1,y1,z1是一個在數組中表示的向量,下面的點表示該值。正如你所看到的,每個x1,y1 ...都有多個點。現在我想將點加起來,使它成爲使用迭代器的向量的總和。我如何使用迭代器來總結所有行?
這樣的:
import numpy
a=numpy.array([0,1,2])
b=numpy.array([3,4,5])
a+b
array([3, 5, 7])
但是這僅僅是2個數組,如果有數百個,那麼你就需要一個迭代器,而不是手動設置陣列嗎?
有什麼預期的輸出? –
某些東西在你粘貼的代碼中被切斷了。 – user2357112
讓我們說a = [x1,y1,z1]和b = [x2,y2,z2]並且總和爲a + b,但我想使用迭代器,因此我可以處理所有行。 – Andy