某些值在数组中被重新分配/更改,在不应该的时候在while循环中

时间:2013-01-15 02:38:36

标签: python arrays loops

首先,我想为我的英语道歉。这不是我的第一语言。

这是我正在开发的一个程序,用于对重复加载三轴试验产生的数据进行排序。第一列是一个段。三段形成一个循环。加载,卸载,暂停。加载大约50个数据点,卸载大约相同,暂停100个。我从121开始加载。对于本节,我需要第4列(索引3)的加载和卸载阶段的最大值。

有我的阵列:

[[  1.21000000e+02   1.00313720e+02   2.00015190e-02 ...,   2.25933480e-01
2.95645450e-01  -3.33373370e-01]
[  1.21000000e+02   1.00318600e+02   2.00071220e-02 ...,   2.25933600e-01
2.95629110e-01  -3.33358880e-01]
[  1.21000000e+02   1.00323490e+02   2.00045150e-02 ...,   2.25932690e-01
2.95642500e-01  -3.33374260e-01]
..., 
[  1.50000000e+02   1.10347900e+02   2.00072340e-02 ...,   2.24460500e-01
2.94727620e-01  -3.38975370e-01]
[  1.50000000e+02   1.10352780e+02   1.99971700e-02 ...,   2.24458930e-01
2.94705120e-01  -3.38966550e-01]
[  1.50000000e+02   1.10357670e+02   2.00063640e-02 ...,   2.24455860e-01
2.94704710e-01  -3.38963510e-01]]
在我通过该循环运行之后

g = np.loadtxt('test.txt')

Sigmad = []


DataCol = np.hsplit(g, g.shape[1])
DataCharge = DataCol[3] #Charge mean load
DataCycle = DataCol[0]


ld = 0 #ligne de début, start line of the load segment
fc = 0 #Fin de chargement, end line of the unload segment
seg1 = DataCycle[0] #Segment initial, numerical value of the first segment

chargemax = []
i = 0


while i < len(DataCycle):

    if DataCycle[i] == seg1 and DataCycle[i+1] == seg1 and DataCycle[i-1] == seg1 - 1:
        ld = i        

    elif DataCycle[i] == seg1 + 2 and DataCycle[i+1] ==  seg1 + 2 and DataCycle[i-1] == seg1 + 1 :

        fc = i
        print seg1
        chargemax.append(np.max(DataCharge[ld:fc]))
        ld = i + 1
        seg1 += 3

    i+=1

Sigmad.append(1000*np.mean(chargemax[:])/aire)

g被修改,我不知道为什么,g现在等于:

[[  1.51000000e+02,   1.00313720e+02,   2.00020000e-02, ...,
      2.25933000e-01,   2.95645000e-01,  -3.33373000e-01],
   [  1.21000000e+02,   1.00318600e+02,   2.00070000e-02, ...,
      2.25934000e-01,   2.95629000e-01,  -3.33359000e-01],
   [  1.21000000e+02,   1.00323490e+02,   2.00050000e-02, ...,
      2.25933000e-01,   2.95642000e-01,  -3.33374000e-01],
   ..., 
   [  1.50000000e+02,   1.10347900e+02,   2.00070000e-02, ...,
      2.24460000e-01,   2.94728000e-01,  -3.38975000e-01],
   [  1.50000000e+02,   1.10352780e+02,   1.99970000e-02, ...,
      2.24459000e-01,   2.94705000e-01,  -3.38967000e-01],
   [  1.50000000e+02,   1.10357670e+02,   2.00060000e-02, ...,
      2.24456000e-01,   2.94705000e-01,  -3.38964000e-01]]

有人可以向我解释为什么会这样做。第4列中的某些值也会被修改。

谢谢!

1 个答案:

答案 0 :(得分:2)

在本节中:

DataCol = np.hsplit(g, g.shape[1])
DataCharge = DataCol[3] #Charge mean load
DataCycle = DataCol[0]

[...]

seg1 = DataCycle[0]

你没有制作副本,你只是给原始数组的视图赋予新名称。所以当你这样做时

seg1 += 3

在循环内部,您正在修改g。例如:

>>> a = np.array([[1,2,3], [4,5,6]])
>>> b = a[0]
>>> b
array([1, 2, 3])
>>> b += 100
>>> b
array([101, 102, 103])
>>> a
array([[101, 102, 103],
       [  4,   5,   6]])

如果您想要副本,可以明确地致电.copy()

>>> a = np.array([[1,2,3], [4,5,6]])
>>> b = a[0].copy()
>>> b
array([1, 2, 3])
>>> b += 1000
>>> b
array([1001, 1002, 1003])
>>> a
array([[1, 2, 3],
       [4, 5, 6]])