I'd like to find the python (numpy is possible)-equivalent of the R rep
and rep_len
functions.
Question 1: Regarding the rep_len
function, say I run,
rep_len(paste('q',1:4,sep=""), length.out = 7)
then the elements of vector ['q1','q2','q3','q4']
will be recycled to fill up 7 spaces and you'll get the output
[1] "q1" "q2" "q3" "q4" "q1" "q2" "q3"
How do I do recycle elements of a list or a 1-d numpy array to fit a predetermined length? From what I've seen numpy's repeat function lets you specify a certain number of reps, but doesn't repeat values to fill a predetermined length.
Question 2: Regarding the rep
function, say I run,
rep(2000:2004, each = 3, length.out = 14)
then the output is
[1] 2000 2000 2000 2001 2001 2001 2002 2002 2002 2003 2003 2003 2004 2004
How could I make this (recycling elements of a list or numpy array to fit a predetermined length and list each element consecutively a predetermined number of times) happen using python?
I apologize if this question has been asked before; I'm totally new to stack overflow and pretty new to programming in general.
Commenting on Psidom's np_rep function, I believe that an additional feature of R's rep function (with the each= parameter) is that it'll recycle elements in the repeated vector until the length specified by length.out is achieved. For example,
returns
. In python, define np_rep as Psidom has defined it,
and call
, the output is
; so the function doesn't recycle to achieve the desired length, but stops after the elements of parameter x have been repeated parameter repeat number of times.
I believe the following should work as a version that incorporates recycling:
The call,
returns
, which is the recycled version that fills up 15 elements.
It'll default to a lambda form of Psidom's np_rep function if length_out doesn't exceed the product of len(x) and repeat.