2016-07-27 16 views
0

次の問題があります。私が達成したい何Append関数で正しい結果を得ることができません

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個のコピーを含みます。リストは変更可能であるため、次のインターレーションからの変更は以前に追加されたリストに反映されます。

リストの現在の状態を追加する場合は、コピーを作成する必要があります。 Try ColNrsperPeriod.append(TempColNrsperPeriod[:])

関連する問題