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],[......
由於某種原因,我不能得到它的工作,並沒有看到有什麼問題。
請讓我知道我應該做些什麼來解決這個問題。非常感謝。
'ColNrsperPeriod.append([TempColNrsperPeriod])'''ColNrsperPeriod'將包含相同列表的N個副本。列表是可變的。如果你想追加目前的列表狀態,你需要創建一個副本。嘗試'ColNrsperPeriod.append(TempColNrsperPeriod [:])'。 –
太好了,謝謝。這確實解決了這個問題。 – AlmostGr