repeat a sequence of numbers N times and incrementally increase the values in the sequence Python
Question:
I have a number sequence as below
sequence = (0,0,1,1,1,1)
I want the number sequence to repeat a specified number of times but incrementally increase the values within the sequence
so if n= 3 then the sequence would go 1,1,2,2,2,2,3,3,4,4,4,4,5,5,6,6,6,6
I can make a sequence like below but the range function is not quite right in this instance
n = 3
CompleteSequence = [j + k for j in range(0, n, 2) for k in sequence]
CompleteSequence
[0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3]
I have tried itertools
import itertools
sequence = (0,0,1,1,1,1)
n=3
list1 = list(itertools.repeat(sequence,n))
list1
[(0, 0, 1, 1, 1, 1), (0, 0, 1, 1, 1, 1), (0, 0, 1, 1, 1, 1)]
How can I go I achieve the pattern I need? a combination of range and itertools?
Answers:
Use range(start, stop, step)
– i.e. with step=2
to only iterate over the odd values:
>>> [[i] * 2 + [i + 1] * 4 for i in range(1, 6, 2)]
[[1, 1, 2, 2, 2, 2], [3, 3, 4, 4, 4, 4], [5, 5, 6, 6, 6, 6]]
Full example:
# they are the same
sequence = (0, 0, 1, 1, 1, 1)
assert [0] * 2 + [1] * 4 == list(sequence)
n = 3
result = [[i] * 2 + [i + 1] * 4 for i in range(1, n * 2, 2)]
print(result)
# in the case you need to unpack values into one list
result = []
for i in range(1, n * 2, 2):
result += [i] * 2 + [i + 1] * 4
print(result)
Result:
[[1, 1, 2, 2, 2, 2], [3, 3, 4, 4, 4, 4], [5, 5, 6, 6, 6, 6]]
[1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6]
You can achieve this pattern with range()
and an increment
variable:
sequence = (0,0,1,1,1,1)
n = 3
CompleteSequence = []
increment = 1
for i in range(1, n+1):
for j in sequence:
CompleteSequence.append(j + increment)
increment += 2
CompleteSequence
now holds:
[1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6]
s = [0,0,1,1,1,1]
n = 3
r = [
i + offset
for offset in range(1, n*2+1, 2)
for i in s
]
You can also use:
from itertools import repeat
sequence = (0, 0, 1, 1, 1, 1)
n = 3
list1 = [i*2+j+1 for i, s in enumerate(repeat(sequence, n)) for j in s]
print(list1)
# Output
[1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6]
Your "incrementally increase" and "the incrementing pattern is what I am mainly trying to achieve" sound like:
from itertools import accumulate
incrementing_pattern = [1, 0, 1, 0, 0, 0]
n = 3
sequence = accumulate(n * incrementing_pattern)
print(*sequence)
Output (Attempt This Online!):
1 1 2 2 2 2 3 3 4 4 4 4 5 5 6 6 6 6
I have a number sequence as below
sequence = (0,0,1,1,1,1)
I want the number sequence to repeat a specified number of times but incrementally increase the values within the sequence
so if n= 3 then the sequence would go 1,1,2,2,2,2,3,3,4,4,4,4,5,5,6,6,6,6
I can make a sequence like below but the range function is not quite right in this instance
n = 3
CompleteSequence = [j + k for j in range(0, n, 2) for k in sequence]
CompleteSequence
[0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3]
I have tried itertools
import itertools
sequence = (0,0,1,1,1,1)
n=3
list1 = list(itertools.repeat(sequence,n))
list1
[(0, 0, 1, 1, 1, 1), (0, 0, 1, 1, 1, 1), (0, 0, 1, 1, 1, 1)]
How can I go I achieve the pattern I need? a combination of range and itertools?
Use range(start, stop, step)
– i.e. with step=2
to only iterate over the odd values:
>>> [[i] * 2 + [i + 1] * 4 for i in range(1, 6, 2)]
[[1, 1, 2, 2, 2, 2], [3, 3, 4, 4, 4, 4], [5, 5, 6, 6, 6, 6]]
Full example:
# they are the same
sequence = (0, 0, 1, 1, 1, 1)
assert [0] * 2 + [1] * 4 == list(sequence)
n = 3
result = [[i] * 2 + [i + 1] * 4 for i in range(1, n * 2, 2)]
print(result)
# in the case you need to unpack values into one list
result = []
for i in range(1, n * 2, 2):
result += [i] * 2 + [i + 1] * 4
print(result)
Result:
[[1, 1, 2, 2, 2, 2], [3, 3, 4, 4, 4, 4], [5, 5, 6, 6, 6, 6]]
[1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6]
You can achieve this pattern with range()
and an increment
variable:
sequence = (0,0,1,1,1,1)
n = 3
CompleteSequence = []
increment = 1
for i in range(1, n+1):
for j in sequence:
CompleteSequence.append(j + increment)
increment += 2
CompleteSequence
now holds:
[1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6]
s = [0,0,1,1,1,1]
n = 3
r = [
i + offset
for offset in range(1, n*2+1, 2)
for i in s
]
You can also use:
from itertools import repeat
sequence = (0, 0, 1, 1, 1, 1)
n = 3
list1 = [i*2+j+1 for i, s in enumerate(repeat(sequence, n)) for j in s]
print(list1)
# Output
[1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6]
Your "incrementally increase" and "the incrementing pattern is what I am mainly trying to achieve" sound like:
from itertools import accumulate
incrementing_pattern = [1, 0, 1, 0, 0, 0]
n = 3
sequence = accumulate(n * incrementing_pattern)
print(*sequence)
Output (Attempt This Online!):
1 1 2 2 2 2 3 3 4 4 4 4 5 5 6 6 6 6