2016-07-27 90 views
0

我遇到以下问题。追加功能,无法获得正确的结果

for som in range(0,len(var)):           # For each number in var (the sequence) 
     if len(PeriodNumbers[som]) != 2:         # When the length of PeriodNumbers is not 2 (2 meaning that there's a column falling in 2 different periods) 
      TempColNrsperPeriod.append(var[som])       # When the if statement is valid we append the column from the sequence. 
      #print TempColNrsperPeriod 
     else:                # When the if statement is false (meaning that lenght is 2): 
      TempColNrsperPeriod.append(var[som])       # We append the current column to the temp list 
      ColNrsperPeriod.append([TempColNrsperPeriod])     # Appending the Temp list to the ColNrsperPeriod list 
     # I do not set the TempcolNrsperPeriod to 0, since I want the next appending to be the cummulative of all columns up until and including the current period. 

var = [42, 30, 45, 61, 62, 80, 81, 101, 83, 66, 49, 24, 15, 25, 52, 39, 53, 68, 54, 41, 29, 28, 17, 26, 51, 50, 48, 34, 64, 65, 84, 67, 35, 47, 33, 63, 82, 100, 119, 141, 140, 115, 137, 136, 113, 159, 183, 213, 185, 158, 209, 231, 208, 230, 207, 206, 154, 178, 126, 104, 105, 129, 130, 155, 179, 153, 106, 127, 177, 151, 152, 128, 107, 108, 89, 90, 91, 111, 157, 131, 156, 182, 212, 160, 134, 133, 110, 109, 132, 92, 93, 74, 72, 73, 56, 57, 58, 75, 76, 94, 112, 135, 161, 186, 162, 138, 114, 95, 97, 77, 60, 43, 44, 31, 32, 46, 78, 116, 98, 118, 120, 99, 117, 139, 163, 187, 251, 260, 252, 259, 258, 266, 253, 222, 173, 202, 172, 171, 197, 238, 237, 219, 195, 167, 217, 193, 164, 216, 235, 246, 215, 191, 192, 233, 232, 214, 165, 194, 166, 142, 168, 198, 236, 248, 250, 249, 257, 263, 247, 234, 218, 196, 169, 143, 144, 121, 148, 146, 170, 199, 223, 240, 241, 242, 201, 203, 174, 175, 204, 228, 176, 205, 245, 244, 254, 262, 261, 239, 221, 220, 79, 59, 96, 103, 124, 147, 123, 125, 150, 149, 200, 224, 225, 226, 243, 227, 255, 190, 189, 188, 86, 87, 70, 71, 88, 38, 37, 36, 23, 14, 8, 16, 9, 10, 27, 40, 69, 85, 102, 20, 19, 18, 11, 3, 6, 12, 4, 5, 13, 7, 2, 1, 55, 184, 210, 211, 181, 180, 229, 264, 265, 256, 122, 145, 22, 21] 

PeriodNumbers =[[1], [1], [1, [2]], [2], [2], [2], [2], [2], [2, [3]], [3], [3], [3], [3], [3, [4]], [4], [4], [4], [4], [4], [4], [4], [4], [4, 5], [5], [5], [5], [5], [5], [5, [6]], [6], [6], [6], [6], [6], [6], [6, [7]], [7], [7], [7], [7], [7], [7], [7], [7], [7, [8]], [8], [8], [8], [8], [8], [8], [8], [8], [8, [9]], [9], [9], [9], [9], [9, 10], [10], [10], [10], [10, [11]], [11], [11], [11], [11], [11], [11], [11, [12]], [12], [12], [12], [12], [12, [13]], [13], [13], [13], [13, [14]], [14], [14], [14], [14], [14], [14], [14], [14, [15]], [15], [15], [15], [15], [15, [16]], [16], [16], [16], [16, [17]], [17], [17], [17], [17], [17], [17, [18]], [18], [18], [18], [18], [18], [18], [18], [18], [18, [19]], [19], [19], [19], [19, [20]], [20], [20], [20], [20], [20], [20], [20], [20], [20], [20, [21]], [21], [21], [21], [21], [21], [21], [21], [21], [21], [21], [21, [22]], [22], [22], [22], [22], [22, [23]], [23], [23], [23], [23], [23, 24], [24], [24], [24], [24], [24], [24], [24], [24, [25]], [25], [25], [25], [25], [25], [25, [26]], [26], [26], [26], [26], [26], [26], [26], [26], [26], [26], [26, [27]], [27], [27], [27], [27], [27, 28], [28], [28], [28], [28], [28], [28, [29]], [29], [29], [29], [29], [29], [29], [29, [30]], [30], [30], [30], [30], [30], [30], [30], [30], [30, [31]], [31], [31], [31], [31], [31], [31], [31, 32], [32], [32], [32], [32], [32, [33]], [33], [33], [33], [33], [33], [33], [33], [33], [33, [34]], [34], [34], [34], [34], [34], [34, [35]], [35], [35], [35], [35], [35], [35], [35, [36]], [36], [36], [36], [36], [36], [36], [36], [36], [36, [37]], [37], [37], [37], [37], [37], [37], [37], [37, [38]], [38], [38], [38], [38], [38], [38], [38], [38], [38], [38, [39]], [39], [39], [39], [39], [39], [39], [39]] 

我想什么来实现的(对于从periodnumbers各个时期,我想有一组所有值向上直到那时和whitin那个时期,而这些成果追加到ColNrsperPeriod当我试着展示下面):

ColNrsperPeriod = [[42, 30, 45],[42, 30, 45, 61, 62, 80, 81, 101, 83],[...... 

由于某种原因,我不能得到它的工作,并没有看到有什么问题。

请让我知道我应该做些什么来解决这个问题。非常感谢。

+0

'ColNrsperPeriod.append([TempColNrsperPeriod])'''ColNrsperPeriod'将包含相同列表的N个副本。列表是可变的。如果你想追加目前的列表状态,你需要创建一个副本。尝试'ColNrsperPeriod.append(TempColNrsperPeriod [:])'。 –

+0

太好了,谢谢。这确实解决了这个问题。 – AlmostGr

回答

1

ColNrsperPeriod.append([TempColNrsperPeriod]) - ColNrsperPeriod将包含相同列表的N个副本。列表是可变的,所以来自下一个interations的变化会传播到之前附加的列表。

如果你想附加列表的当前状态,你需要创建一个副本。 尝试ColNrsperPeriod.append(TempColNrsperPeriod[:])