I am Python newbie and trying to replicate an R script of the following form in Python.
# set value for k
k <- 3
# script 1 R
cnt <- c(1,-1, rep(0,k-2))
print(cnt)
1 -1 0
# script 2 R
for (i in 2:(k-1)) {
cnt <- c(cnt, c(rep(0,(i-1)),1,-1,rep(0,(k-i-1))))
}
print(cnt)
1 -1 0 0 1 -1
In the script 2 the outcome of script 1 prefixed via concatenation. Given that Python doesn't have a direct equivalent to Rs rep() function, this
is what I have attempted to do, using numpy.repeat.
For R script 1, I did the following, which got me close to the desired outcome 1 -1 0,
# code 1 Python
pcnt = np.array([1, -1, np.repeat('0', k-2)], dtype=int)
print(pcnt)
[ 1 -1 0]
but with a DeprecationWarning: setting an array element with a sequence. This was supported in some cases where the elements are arrays with a single element. For example `np.array([1, np.array([2])], dtype=int)`. In the future this will raise the same ValueError as `np.array([1, [2]], dtype=int).
For R script 2 I tried the following, but excluded the concatenation part with the intention of concatenating them after.
# code 2 Python
for i in range(2, k-1+1):
pcnt2 = np.array([np.repeat(0, (i-1)), 1, -1, np.repeat(0, (k-i-1))], dtype='int')
print(pcnt2)
The above code raises a ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (4,) + inhomogeneous part.
However, when I change dtype='object' like so
for i in range(2, k-1+1):
pcnt3 = np.array([np.repeat(0, (i-1)), 1, -1, np.repeat(0, (k-i-1))], dtype='object')
print(pcnt3)
I get:
array([array([0]), 1, -1, array([], dtype=int32)], dtype=object)
What I need help with is how to get two separate arrays from both Python codes, with code 1 Python resulting in the following output 1 -1 0 and code 2 Python outputting 0 1 -1