我有一个数组
array([[ 1.00000000e+00, 1.08020000e+04, 1.08070000e+04],
[ 1.00000000e+00, 1.08760000e+04, 1.08810000e+04],
[ 1.00000000e+00, 1.09520000e+04, 1.09570000e+04],
[ 1.00000000e+00, 1.18130000e+04, 1.18180000e+04],
[ 1.00000000e+00, 1.21400000e+04, 1.21450000e+04],
[ 1.00000000e+00, 1.26300000e+04, 1.26350000e+04],
[ 1.00000000e+00, 1.50800000e+04, 1.50850000e+04],
[ 1.00000000e+00, 1.56590000e+04, 1.56640000e+04],
[ 1.00000000e+00, 1.70740000e+04, 1.70790000e+04]])
我必须减去第2列的row1和row2,并将结果添加到row1中的新列。愿有人帮助实现这一目标。
result1 =row2-row1
result2 =row3-row2
我想循环迭代第2列中的所有值。
我尝试写这个但不能执行正确的切片:
testfile = numpy.loadtxt("numpy_test.txt", skiprows=1, usecols=(0,1,2))
i=1
j=i-1
k=1
for array in testfile:
print array[i][k]-array[j][k] #result = arr[1][1] - arr[0][1]
i+=1
j+=1
如何迭代一个循环,这是我的问题,你已经帮助我理解了我们如何编码元素。
答案 0 :(得分:0)
我认为我的答案不够好。但是,我不能评论,所以我必须把我的想法放在答案中。
让你的数组成为一个。我的解决方案是:
for item in a:
item[0] = item[1] - item[0]
# you can add item[1] = item[2] - item[1] if you want
这将返回结果。
答案 1 :(得分:0)
我想你想要这样的东西?:
array = [[ 1.00000000e+00, 1.08020000e+04, 1.08070000e+04],
[ 1.00000000e+00, 1.08760000e+04, 1.08810000e+04],
[ 1.00000000e+00, 1.09520000e+04, 1.09570000e+04],
[ 1.00000000e+00, 1.18130000e+04, 1.18180000e+04],
[ 1.00000000e+00, 1.21400000e+04, 1.21450000e+04],
[ 1.00000000e+00, 1.26300000e+04, 1.26350000e+04],
[ 1.00000000e+00, 1.50800000e+04, 1.50850000e+04],
[ 1.00000000e+00, 1.56590000e+04, 1.56640000e+04],
[ 1.00000000e+00, 1.70740000e+04, 1.70790000e+04]]
result1 = array[1][1] - array[0][1] # 1.08760000e+04 - 1.08020000e+04
result2 = array[2][1] - array[1][1] # 1.09520000e+04 - 1.08760000e+04
答案 2 :(得分:0)
可以像多维数组一样访问Numpy矩阵:
result1 = arr[1][1] - arr[0][1]
result2 = arr[2][1] - arr[1][1]
要添加新列,请创建新的临时数组,并在将arr
的内容复制到其中后,将所有内容复制回arr
:
temp = np.zeros((arr.shape[0], arr.shape[1]+1))
temp[:,:-1] = arr
temp[0][temp.shape[1]+1] = result1 + result2
总而言之:
import numpy as np
arr = np.array([[ 1.00000000e+00, 1.08020000e+04, 1.08070000e+04],
[ 1.00000000e+00, 1.08760000e+04, 1.08810000e+04],
[ 1.00000000e+00, 1.09520000e+04, 1.09570000e+04],
[ 1.00000000e+00, 1.18130000e+04, 1.18180000e+04],
[ 1.00000000e+00, 1.21400000e+04, 1.21450000e+04],
[ 1.00000000e+00, 1.26300000e+04, 1.26350000e+04],
[ 1.00000000e+00, 1.50800000e+04, 1.50850000e+04],
[ 1.00000000e+00, 1.56590000e+04, 1.56640000e+04],
[ 1.00000000e+00, 1.70740000e+04, 1.70790000e+04]])
result1 = arr[1][1] - arr[0][1]
result2 = arr[2][1] - arr[1][1]
temp = np.zeros((arr.shape[0], arr.shape[1]+1))
temp[:,:-1] = arr
temp[0][temp.shape[1]-1] = result1 + result2
print temp
给出:
[[ 1.00000000e+00 1.08020000e+04 1.08070000e+04 1.50000000e+02]
[ 1.00000000e+00 1.08760000e+04 1.08810000e+04 0.00000000e+00]
[ 1.00000000e+00 1.09520000e+04 1.09570000e+04 0.00000000e+00]
[ 1.00000000e+00 1.18130000e+04 1.18180000e+04 0.00000000e+00]
[ 1.00000000e+00 1.21400000e+04 1.21450000e+04 0.00000000e+00]
[ 1.00000000e+00 1.26300000e+04 1.26350000e+04 0.00000000e+00]
[ 1.00000000e+00 1.50800000e+04 1.50850000e+04 0.00000000e+00]
[ 1.00000000e+00 1.56590000e+04 1.56640000e+04 0.00000000e+00]
[ 1.00000000e+00 1.70740000e+04 1.70790000e+04 0.00000000e+00]]
答案 3 :(得分:0)
我就是这样做的:
import numpy as np
A = np.array([[ 1.0, 1.0802e4, 1.0807e4],
[ 1.0, 1.0876e4, 1.0881e4],
[ 1.0, 1.0952e4, 1.0957e4],
[ 1.0, 1.1813e4, 1.1818e4],
[ 1.0, 1.2140e4, 1.2145e4],
[ 1.0, 1.2630e4, 1.2635e4],
[ 1.0, 1.5080e4, 1.5085e4],
[ 1.0, 1.5659e4, 1.5664e4],
[ 1.0, 1.7074e4, 1.7079e4]])
# First add a column to the array
m, n = A.shape
new_column = np.zeros((m, 1))
A = np.hstack((A, new_column))
# Now loop over the rows and calculate differences
for i in range(m-1):
A[i, 3] = A[i+1, 1] - A[i, 1]
# Or alternatively, without a loop
A[:-1, 3] = np.diff(A[:, 1])
请注意,您不能将Numpy数组直接迭代为for array in testfile
,因为您需要来自多行的值。