I was learning itertools and encountered an interesting problem.
How to get the following result?
nums = [1,2,3,4]
# logic: i+1 th number is repeated i times.
2 is repeated 1 times and 4 is repeated 3 times.
required = [2,4,4,4]
My attempt
import itertools
nums = [1,2,3,4]
nums[::2] # [1,3]
nums[1::2] # [2,4]
* 2 is repeated 1 times
* 4 is repeated 3 times and makes [2,4,4,4]
list(itertools.starmap(itertools.repeat, zip(nums[::2],nums[1::2])))
Gives,
[repeat(1, 2), repeat(3, 4)]
How to get:
required = [2,4,4,4]
Required
From: [1,2,3,4]
To: [2,4,4,4]
We can use itertools, list comp and numpy.
With emphasis on itertools, since I am learning itertools functions
such as repeat, starmap and so on.