我在 python 中使用 Numpy 来读取 csv 文件:
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 个数组,如果有数百个数组,那么您需要一个迭代器而不是手动设置数组吗?