列表中存储的先前数据会随着其他数据的添加而更改

时间:2018-12-08 16:03:34

标签: python

TL; DR 当我追加(或插入)数据时,“错误数据”也会被追加,列表中以前存储的数据似乎也会发生变化。

我在代码中遇到一个非常奇怪的错误,对我来说真的没有意义。

我正在读取一个CSV文件,并且其数据存储为列表(ratio),从该列表中,我使用np.mean()np.std()来计算均值和标准偏差,以及将它们存储在列表中。在这些计算中,我调用一个函数来检查很多事情,但重要的是:

状态发生变化时,该函数将返回一个列表(称为status,其中第一项为1。如果状态没有变化,则返回的结果也将是一个列表,但第一项为0 。到目前为止,这里的所有内容都可以使用。

因此,我决定将指示状态更改的所有返回(来自提到的函数)(称为crossed)添加到新列表(称为if status[0] == 1:)中,因此print(status)但是不会添加到列表中的数据预期的。

请注意:我在检查status[0] == 1之后添加了一个.append(status):它按预期返回,下一行是status = [0,0,0,0,0,0,0] crossed = [] for i in range((len(ratio)-periods+1)): if i == 0: #Workaround: When i = 0, ratio[-periods-0:0] won't work mean.append(np.mean(ratio[-periods-i:])) std.append(np.std(ratio[-periods-i:])) lower_std.append(mean[-1]-std[-1]) status = check_ratio(ratio[-periods],mean[-1],lower_std[-1],i,status) if status[0] == 1: crossed.append(status) else: mean.append(np.mean(ratio[-periods-i:-i])) std.append(np.std(ratio[-periods-i:-i])) lower_std.append(mean[-1]-std[-1]) status = check_ratio(ratio[-periods-i],mean[-1],lower_std[-1],i,status) if status[0] == 1: print (status) crossed.append(status) print(crossed) ,而且根本不会附加打印的内容。

print(status)

check_ratio()函数返回包含大量数据的列表,但第一项始终为0或1。 [1, 1, 54, 1.138758865248227, 1.136314650125257, 1.1337690420509274, 1.1312234339765979] [1, 2, 57, 1.1353429985855727, 1.136314650125257, 1.1337690420509274, 1.1312234339765979] [1, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979] [1, -1, 78, 1.1309345594525235, 0, 0, 0] [1, 1, 127, 1.130847142192477, 1.1304628293233858, 1.1294503731447396, 1.1284379169660932] [1, 2, 131, 1.1303973913355072, 1.1304628293233858, 1.1294503731447396, 1.1284379169660932] [1, 0, 135, 1.1335466666666667, 0, 0, 0] [1, 1, 138, 1.1308163809185396, 1.1296433002012238, 1.128433107660401, 1.1272229151195785] [1, 2, 139, 1.1282648093529015, 1.1296433002012238, 1.128433107660401, 1.1272229151195785] [1, -1, 140, 1.1270390684629994, 0, 0, 0] [1, 1, 142, 1.1255687149255582, 1.1256917189689608, 1.1233910776545415, 1.1210904363401222] [1, 2, 143, 1.1254230680630128, 1.1256917189689608, 1.1233910776545415, 1.1210904363401222] [1, 3, 150, 1.1225412014885698, 1.1256917189689608, 1.1233910776545415, 1.1210904363401222] [1, -1, 154, 1.1210447814197948, 0, 0, 0] [1, 1, 197, 1.1171276991444958, 1.1161855800851872, 1.1132670986656799, 1.1103486172461723] [1, 2, 198, 1.1127123608670182, 1.1161855800851872, 1.1132670986656799, 1.1103486172461723] [1, 0, 203, 1.1246100837937574, 0, 0, 0] [1, 1, 284, 1.1390154874100347, 1.1381523185174933, 1.1368941197303815, 1.1356359209432696] [1, 0, 287, 1.1418469131706104, 0, 0, 0] [1, 1, 292, 1.137294829075651, 1.136934972982161, 1.1354744212606314, 1.134013869539102] [1, 0, 297, 1.1415675686816016, 0, 0, 0] [1, 1, 405, 1.135509186838826, 1.1350790378560178, 1.1335324241438416, 1.1319858104316656] [1, 2, 407, 1.1339004993526909, 1.1350790378560178, 1.1335324241438416, 1.1319858104316656] [1, 3, 414, 1.1330645161290323, 1.1350790378560178, 1.1335324241438416, 1.1319858104316656] [1, 0, 429, 1.1365694764439502, 0, 0, 0] [1, 1, 442, 1.1273146097280056, 1.1267225301265933, 1.1244185615555562, 1.1221145929845189] [1, 0, 443, 1.1352307739239194, 0, 0, 0] [1, 1, 487, 1.1294793579340165, 1.1275050776354896, 1.1255123932368172, 1.1235197088381448] [1, 0, 490, 1.1343246592317224, 0, 0, 0] [1, 1, 507, 1.1273536042898056, 1.1260499911131632, 1.1238715385874056, 1.121693086061648] [1, 2, 509, 1.1253301757901448, 1.1260499911131632, 1.1238715385874056, 1.121693086061648] [1, 0, 521, 1.1303555916720376, 0, 0, 0] [1, 1, 526, 1.1238224560807943, 1.120965441404663, 1.1181032018014265, 1.11524096219819] [1, 0, 528, 1.1290014064697609, 0, 0, 0] [1, 1, 580, 1.1282299098894226, 1.1262199790650198, 1.1233324063904733, 1.1204448337159267] [1, 2, 586, 1.1258169934640523, 1.1262199790650198, 1.1233324063904733, 1.1204448337159267] [1, 3, 599, 1.1229769929859992, 1.1262199790650198, 1.1233324063904733, 1.1204448337159267] [1, 0, 605, 1.131025825922068, 0, 0, 0] 的返回结果是这样的:(请注意,按预期方式,每个列表都以1开头

print(crossed)

现在这是[[0, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [0, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [0, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [1, -1, 78, 1.1309345594525235, 0, 0, 0], [0, 2, 131, 1.1303973913355072, 1.1304628293233858, 1.1294503731447396, 1.1284379169660932], [0, 2, 131, 1.1303973913355072, 1.1304628293233858, 1.1294503731447396, 1.1284379169660932], [1, 0, 135, 1.1335466666666667, 0, 0, 0], [1, 2, 139, 1.1282648093529015, 1.1296433002012238, 1.128433107660401, 1.1272229151195785], [1, 2, 139, 1.1282648093529015, 1.1296433002012238, 1.128433107660401, 1.1272229151195785], [1, -1, 140, 1.1270390684629994, 0, 0, 0], [0, 3, 150, 1.1225412014885698, 1.1256917189689608, 1.1233910776545415, 1.1210904363401222], [0, 3, 150, 1.1225412014885698, 1.1256917189689608, 1.1233910776545415, 1.1210904363401222], [0, 3, 150, 1.1225412014885698, 1.1256917189689608, 1.1233910776545415, 1.1210904363401222], [1, -1, 154, 1.1210447814197948, 0, 0, 0], [0, 2, 198, 1.1127123608670182, 1.1161855800851872, 1.1132670986656799, 1.1103486172461723], [0, 2, 198, 1.1127123608670182, 1.1161855800851872, 1.1132670986656799, 1.1103486172461723], [1, 0, 203, 1.1246100837937574, 0, 0, 0], [0, 1, 284, 1.1390154874100347, 1.1381523185174933, 1.1368941197303815, 1.1356359209432696], [1, 0, 287, 1.1418469131706104, 0, 0, 0], [0, 1, 292, 1.137294829075651, 1.136934972982161, 1.1354744212606314, 1.134013869539102], [1, 0, 297, 1.1415675686816016, 0, 0, 0], [0, 3, 414, 1.1330645161290323, 1.1350790378560178, 1.1335324241438416, 1.1319858104316656], [0, 3, 414, 1.1330645161290323, 1.1350790378560178, 1.1335324241438416, 1.1319858104316656], [0, 3, 414, 1.1330645161290323, 1.1350790378560178, 1.1335324241438416, 1.1319858104316656], [1, 0, 429, 1.1365694764439502, 0, 0, 0], [1, 1, 442, 1.1273146097280056, 1.1267225301265933, 1.1244185615555562, 1.1221145929845189], [1, 0, 443, 1.1352307739239194, 0, 0, 0], [0, 1, 487, 1.1294793579340165, 1.1275050776354896, 1.1255123932368172, 1.1235197088381448], [1, 0, 490, 1.1343246592317224, 0, 0, 0], [0, 2, 509, 1.1253301757901448, 1.1260499911131632, 1.1238715385874056, 1.121693086061648], [0, 2, 509, 1.1253301757901448, 1.1260499911131632, 1.1238715385874056, 1.121693086061648], [1, 0, 521, 1.1303555916720376, 0, 0, 0], [0, 1, 526, 1.1238224560807943, 1.120965441404663, 1.1181032018014265, 1.11524096219819], [1, 0, 528, 1.1290014064697609, 0, 0, 0], [0, 3, 599, 1.1229769929859992, 1.1262199790650198, 1.1233324063904733, 1.1204448337159267], [0, 3, 599, 1.1229769929859992, 1.1262199790650198, 1.1233324063904733, 1.1204448337159267], [0, 3, 599, 1.1229769929859992, 1.1262199790650198, 1.1233324063904733, 1.1204448337159267], [1, 0, 605, 1.131025825922068, 0, 0, 0]] 返回:

status = check_ratio(ratio[-periods-i],mean[-1],lower_std[-1],i,status)
if status[0] == 1:
    crossed.append(status)
    print('>> Appended:',crossed[-1])
    print(crossed)

它们应该完全匹配,但是实际上只有几个条目匹配,并且某些数据以某种方式重复了(请参阅print(crossed)的第1,2,3行以及5,6和8,9行)那可能吗?它从字面上打印应该添加的行,并附加另一行。

因此,我决定在每次添加附件时都尝试打印,结果确实很奇怪。

status[0]==1

我剪切了返回到前几行和最后一行的内容。这是我观察到的:

  • 第一行是正确的(Appended: [1, 1, 54, 1.138758865248227, 1.136314650125257, 1.1337690420509274, 1.1312234339765979] [[1, 1, 54, 1.138758865248227, 1.136314650125257, 1.1337690420509274, 1.1312234339765979]] Appended: [1, 2, 57, 1.1353429985855727, 1.136314650125257, 1.1337690420509274, 1.1312234339765979] [[1, 2, 57, 1.1353429985855727, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [1, 2, 57, 1.1353429985855727, 1.136314650125257, 1.1337690420509274, 1.1312234339765979]] Appended: [1, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979] [[1, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [1, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [1, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979]] Appended: [1, -1, 78, 1.1309345594525235, 0, 0, 0] [[0, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [0, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [0, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [1, -1, 78, 1.1309345594525235, 0, 0, 0]] Appended: [1, 1, 127, 1.130847142192477, 1.1304628293233858, 1.1294503731447396, 1.1284379169660932] [[0, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [0, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [0, 3, 68, 1.1337522123893804, 1.136314650125257, 1.1337690420509274, 1.1312234339765979], [1, -1, 78, 1.1309345594525235, 0, 0, 0], [1, 1, 127, 1.130847142192477, 1.1304628293233858, 1.1294503731447396, 1.1284379169660932]] Appended: [1, 2, 131, 1.1303973913355072, 1.1304628293233858, 1.1294503731447396, 1.1284379169660932] ),并附加在行中

  • 第二行是正确的,但是第一行(先前已添加)被第二行的2份副本替换了(如何?)

  • 第三行是正确的,但是先前的数据已被第三行的3个副本替换(第二行的行为相同)

  • 第四行是正确的,但是之前的数据已替换掉,但是每个(先前的条目)的第一项都更改为0。(请参阅附录4下方的列表) )

到此为止,我得到的列表毫无意义。

css: [
  '@/assets/css/reset.css',
  '@/assets/css/main.scss',
],
modules: [
 [
  'nuxt-sass-resources-loader',
  ['./assets/css/main.scss'],
  ['./assets/css/reset.css']
 ]
],

这对我来说真的是0。有人可以看到是什么触发了这种奇怪的行为吗?

1 个答案:

答案 0 :(得分:1)

如果将列表(或其他可变容器)添加到列表中,并且该列表后来发生了突变,则该突变也将应用于该容器,因为它是相同的列表。如果存在以后发生变异的风险,请传递(或存储)列表的副本以防止此类问题(称为别名)。

# A function that accepts a list and changes it
>>> def f(sts):
...     sts[-1] = sts[-1] * 2
...     return [x for x in sts]
... 
>>> sts = [1]
>>> out = []
# Observe how the _out_ list is not what we expect (ascending powers of two)
>>> for i in range(5):
...     sts = f(sts)
...     out.append(sts)
...     print(out)
... 
[[2]]
[[4], [4]]
[[4], [8], [8]]
[[4], [8], [16], [16]]
[[4], [8], [16], [32], [32]]


# If we pass a copy of sts, we get the expected output.
>>> sts = [1]
>>> out = []
>>> for i in range(5):
...     sts = f(sts[:])
...     out.append(sts)
...     print(out)
... 
[[2]]
[[2], [4]]
[[2], [4], [8]]
[[2], [4], [8], [16]]
[[2], [4], [8], [16], [32]]