Reputation: 13
Here is my code, which is used to provide row numbers in excel:
clusters = 3
rowRef = [9, 12, 15]
x = 1
while x < clusters:
rowRef[x:] = [a + 1 for a in rowRef]
x = x + 1
The output I'm looking to achieve is:
So with each iteration of the loop it changes one less item from the list. However, the output I get is:
[9, 10, 10, 11, 14, 17]
What do I need to change for this to work?
Upvotes: 1
Views: 262
Reputation: 3012
Here's a mathematical shortcut to achieve your desired output with more efficient code.
rowRef = [9, 12, 15]
for i in range(1, len(rowRef)):
rowRef[i] += i
print(rowRef)
Upvotes: 0
Reputation: 42746
You lack one more slicing on your list comprehension:
rowRef[x:] = [a + 1 for a in rowRef[x:]]
Otherwise, you are iterating through the whole list instead of the remaining list to process each time.
Here you have the live example
Upvotes: 2