简体   繁体   English

在 python 中重复 function 类似于 R

[英]Repeat function in python similar to R

The question is: Is there a repeat function in python similar to R rep function?问题是:python 中是否有重复的 function 类似于 R rep ZC1C425268E68385D1ABZ4A? R have a powerful rep function as follows: R 有一个强大的代表 function 如下:

rep(x, times = 1, length.out = NA, each = 1)
  • x: a vector x:一个向量

  • times: an integer-valued vector giving the (non-negative) number of times to repeat each element if of length length(x), or to repeat the whole vector if of length 1. Negative or NA values are an error. times:一个整数值向量,如果长度为 length(x),则给出重复每个元素的(非负)次数,如果长度为 1,则重复整个向量。负值或 NA 值是错误的。 A double vector is accepted, other inputs being coerced to an integer or double vector.接受双向量,其他输入被强制为 integer 或双向量。

  • length.out: non-negative integer. length.out:非负 integer。 The desired length of the output vector. output 向量的所需长度。 Other inputs will be coerced to a double vector and the first element taken.其他输入将被强制转换为双向量并采用第一个元素。 Ignored if NA or invalid.如果 NA 或无效则忽略。

  • each: non-negative integer.每个:非负 integer。 Each element of x is repeated each times. x 的每个元素每次都重复。 Other inputs will be coerced to an integer or double vector and the first element taken.其他输入将被强制转换为 integer 或双向量并采用第一个元素。 Treated as 1 if NA or invalid.如果 NA 或无效,则视为 1。

Some examples are as follows:一些例子如下:

R code
> letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
[23] "w" "x" "y" "z"
#Example 1
> rep(letters[1:3],times=c(1,2,4))
[1] "a" "b" "b" "c" "c" "c" "c"
#Example 2
> rep(letters[1:3],each=2,len=15)
 [1] "a" "a" "b" "b" "c" "c" "a" "a" "b" "b" "c" "c" "a" "a" "b"
#repeat a:c each element 2 until length be 15
#Example 3
> rep(letters[1:3],each=3,times=2)
 [1] "a" "a" "a" "b" "b" "b" "c" "c" "c" "a" "a" "a" "b" "b" "b" "c" "c" "c"
#repeat a:c each element 3  and repeat this 2 times
#Example 4
> rep(letters[c(TRUE,FALSE)],each=2)
 [1] "a" "a" "c" "c" "e" "e" "g" "g" "i" "i" "k" "k" "m" "m" "o" "o" "q" "q" "s" "s" "u" "u"
[23] "w" "w" "y" "y"
#Example 5
> rep(letters[c(TRUE,FALSE,TRUE,FALSE,FALSE)],each=2)
 [1] "a" "a" "c" "c" "f" "f" "h" "h" "k" "k" "m" "m" "p" "p" "r" "r" "u" "u" "w" "w" "z" "z"
#Example 6
> rep(letters[c(TRUE,FALSE,TRUE,FALSE,FALSE)],each=2,len=25)
[1] "a" "a" "c" "c" "f" "f" "h" "h" "k" "k" "m" "m" "p" "p" "r" "r" "u" "u" "w" "w" "z" "z"
[23] "a" "a" "c"

In python I just found functions that repeat each element of an array, that is, repeat elements of an array 4 times.在 python 中,我刚刚找到了重复数组的每个元素的函数,即重复数组的元素 4 次。

Python
import numpy as np
import string
letters =string.ascii_lowercase
letters =list(letters)
print(np.repeat(letters, 2))
['a' 'a' 'b' 'b' 'c' 'c' 'd' 'd' 'e' 'e' 'f' 'f' 'g' 'g' 'h' 'h' 'i' 'i'
 'j' 'j' 'k' 'k' 'l' 'l' 'm' 'm' 'n' 'n' 'o' 'o' 'p' 'p' 'q' 'q' 'r' 'r'
 's' 's' 't' 't' 'u' 'u' 'v' 'v' 'w' 'w' 'x' 'x' 'y' 'y' 'z' 'z']
 
print(np.repeat(['a','b'], [1,2]))
['a' 'b' 'b']

Is it possible to use numpy.repeat like rep function in R(see example 4,5,6)?是否可以在 R 中使用 numpy.repeat 像 rep function 一样(参见示例 4、5、6)? if not?如果不? Is there a function that does the same as R's rep?是否有与 R 的代表相同的 function? If not how to create one?如果不是如何创建一个?

EDIT: I can make all examples as follows (still working to make a flexible function to do this):编辑:我可以制作如下所有示例(仍在努力制作灵活的 function 来执行此操作):

    #Python code
    import numpy, itertools, string
    letters =string.ascii_lowercase
    letters =list(letters)
    #Example 1
    #> rep(letters[1:3],times=c(1,2,4))
    #[1] "a" "b" "b" "c" "c" "c" "c"
    lttrs=[letters[i] for i in [0,1,2]]
    print(lttrs)
    # ['a', 'b', 'c']
    rd=numpy.repeat(lttrs,[1,2,4])
    print(rd)
    #['a' 'b' 'b' 'c' 'c' 'c' 'c']
    ########################################################
    
    
    #Example 2
    #> rep(letters[1:3],each=2,len=15)
    # [1] "a" "a" "b" "b" "c" "c" "a" "a" "b" "b" "c" "c" "a" "a" "b"
    #repeat a:c each element 2 until length be 15
    
    input=itertools.cycle(numpy.repeat(lttrs, 2))
    rd=list(itertools.islice(itertools.cycle(input), 15))
    print(rd)
    #['a', 'a', 'b', 'b', 'c', 'c', 'a', 'a', 'b', 'b', 'c', 'c', 'a', 'a', 'b']
    ######################################################
    
    
    #Example 3
    #> rep(letters[1:3],each=3,times=2)
    # [1] "a" "a" "a" "b" "b" "b" "c" "c" "c" "a" "a" "a" "b" "b" "b" "c" "c" "c"
    #repeat a:c each element 3  and repeat this 2 times
    
    result_numpy=numpy.repeat(lttrs,3)
    result_itertools=list(itertools.repeat(result_numpy,2))
    rd= list(itertools.chain(*list(result_itertools)))
    print(rd)
    
    # ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
    ######################################################
    
    
    #Example 4
    #> rep(letters[c(TRUE,FALSE)],each=2)
    # [1] "a" "a" "c" "c" "e" "e" "g" "g" "i" "i" "k" "k" "m" "m" "o" "o" "q" "q" "s" "s" "u" "u"
    #[23] "w" "w" "y" "y"
    
    def which_is_true(indexTF):
        return [i for i, x in enumerate(indexTF) if x]
    
      
    def index_TF(x,index=[True]):
        if(len(index)<len(x)):
            index_final=list(itertools.islice(itertools.cycle(index), len(x)))
        else:
            index_final=index[:len(x)]
        return [x[i] for i in list(which_is_true(index_final))]
    
    
    lttrs_TF=index_TF(letters,[True,False])
    input=numpy.repeat(lttrs_TF, 2)
    rd=list(input)
    print(rd)
    #['a', 'a', 'c', 'c', 'e', 'e', 'g', 'g', 'i', 'i', 'k', 'k', 'm', 'm', 'o', 'o', 'q', 'q', 's', 's', 'u', 'u', 'w', 'w', 'y', 'y']
    
    
    #####################################
    #Example 5
    #> rep(letters[c(TRUE,FALSE,TRUE,FALSE,FALSE)],each=2)
    # [1] "a" "a" "c" "c" "f" "f" "h" "h" "k" "k" "m" "m" "p" "p" "r" "r" "u" "u" "w" "w" "z" "z"
    lttrs_TF=index_TF(letters,[True,False,True,False,False])
    input=numpy.repeat(lttrs_TF, 2)
    rd=list(input)
    print(rd)
    
    #['a', 'a', 'c', 'c', 'f', 'f', 'h', 'h', 'k', 'k', 'm', 'm', 'p', 'p', 'r', 'r', 'u', 'u', 'w', 'w', 'z', 'z']
    
    #Example 6
    #> rep(letters[c(TRUE,FALSE,TRUE,FALSE,FALSE)],each=2,len=25)
    #[1] "a" "a" "c" "c" "f" "f" "h" "h" "k" "k" "m" "m" "p" "p" "r" "r" "u" "u" "w" "w" "z" "z"
    #[23] "a" "a" "c"
    lttrs_TF=index_TF(letters,[True,False,True,False,False])
    input=itertools.cycle(numpy.repeat(lttrs_TF, 2))
    output=list(itertools.islice(itertools.cycle(input), 25))
    print(output)
    #  ['a', 'a', 'c', 'c', 'f', 'f', 'h', 'h', 'k', 'k', 'm', 'm', 'p', 'p', 'r', 'r', 'u', 'u', 'w', 'w', 'z', 'z', 'a', 'a', 'c']

Its not quite clear what you want to achieve.它不太清楚你想要实现什么。 You can use slicing to get an equivalent output in numpy like Example 4, or you can use indexing with a mask to get your desired results (Example 5).您可以像示例 4 一样使用slicing在 numpy 中获得等效的 output,或者您可以使用带有掩码的indexing来获得所需的结果(示例 5)。 There is not an exact equivalent function in python. python 中没有完全等效的 function。

# Example 4
import numpy as np
import string
letters =string.ascii_lowercase
letters =list(letters)

print(np.repeat(letters[::2],2))

['a' 'a' 'c' 'c' 'e' 'e' 'g' 'g' 'i' 'i' 'k' 'k' 'm' 'm' 'o' 'o' 'q' 'q'
 's' 's' 'u' 'u' 'w' 'w' 'y' 'y']

# Example 5
import numpy as np
import string
letters =string.ascii_lowercase  
letters = np.array(list(letters))

indicies = [(0,2,5,7,10,12,15,17,20,22,25)]

print(np.repeat(letters[indicies],2))

['a' 'a' 'c' 'c' 'f' 'f' 'h' 'h' 'k' 'k' 'm' 'm' 'p' 'p' 'r' 'r' 'u' 'u'
 'w' 'w' 'z' 'z']
import numpy, itertools, string
letters =string.ascii_lowercase
letters =list(letters)
lttrs=[letters[i] for i in [0,1,2]]
print(lttrs)
def which_is_true(indexTF):
    return [i for i, x in enumerate(indexTF) if x]

  
def index_TF(x,index=[True]):
    if(len(index)<len(x)):
        index_final=list(itertools.islice(itertools.cycle(index), len(x)))
    else:
        index_final=index[:len(x)]
    return [x[i] for i in list(which_is_true(index_final))]


def rep_times(x, times=1,each=1):
    result_numpy=numpy.repeat(x,each)
    result_itertools=list(itertools.repeat(result_numpy,times))
    return list(itertools.chain(*list(result_itertools)))

def rep_lengthout(x, lengthout=None,each=1):
    
    input=itertools.cycle(numpy.repeat(x, each))
    if lengthout:
        output=list(itertools.islice(itertools.cycle(input), lengthout))
    else:
        lengthout_all=len(x)*each
        output=list(itertools.islice(itertools.cycle(input), lengthout_all))
    return output


def rep_times_TF(x, times = 1, each = 1,index=[True]):
    x_index_TrueFalse=index_TF(x,index=index)
    ret_v=rep_times(x_index_TrueFalse, times=times,each=each)
    return(list(ret_v))

def rep_lengthout_TF(x, lengthout=None,each=1,index=[True]):
    x_index_TrueFalse=index_TF(x,index=index)
    output=rep_lengthout(x_index_TrueFalse, lengthout=lengthout,each=each)
    return output
  
rep_times_TF(lttrs, times = 2, each = 2,index=[True,False])
rep_lengthout_TF(lttrs, lengthout=None , each = 2,index=[True,False])
###########################################

#Example 1
#> rep(letters[1:3],times=c(1,2,4))
#[1] "a" "b" "b" "c" "c" "c" "c"
rd=numpy.repeat(lttrs,[1,2,4])
print(rd)
rep_times_TF(lttrs, times = 1, each = [1,2,4],index=[True])
##################################################


#Example 2
#> rep(letters[1:3],each=2,len=15)
# [1] "a" "a" "b" "b" "c" "c" "a" "a" "b" "b" "c" "c" "a" "a" "b"
#repeat a:c each element 2 until length be 15

input=itertools.cycle(numpy.repeat(lttrs, 2))
rd=list(itertools.islice(itertools.cycle(input), 15))
print(rd)

rep_lengthout_TF(lttrs, lengthout=15 , each = 2,index=[True])

###################################################



#Example 3
#> rep(letters[1:3],each=3,times=2)
# [1] "a" "a" "a" "b" "b" "b" "c" "c" "c" "a" "a" "a" "b" "b" "b" "c" "c" "c"
#repeat a:c each element 3  and repeat this 2 times

result_numpy=numpy.repeat(lttrs,3)
result_itertools=list(itertools.repeat(result_numpy,2))
rd= list(itertools.chain(*list(result_itertools)))
print(rd)

rep_times_TF(lttrs, times = 2, each = 3,index=[True])


######################################################


#Example 4
#> rep(letters[c(TRUE,FALSE)],each=2)
# [1] "a" "a" "c" "c" "e" "e" "g" "g" "i" "i" "k" "k" "m" "m" "o" "o" "q" "q" "s" "s" "u" "u"
#[23] "w" "w" "y" "y"

lttrs_TF=index_TF(letters,[True,False])
input=numpy.repeat(lttrs_TF, 2)
rd=list(input)
print(rd)

rep_times_TF(letters, times = 1, each = 2,index=[True,False])

#####################################
#Example 5
#> rep(letters[c(TRUE,FALSE,TRUE,FALSE,FALSE)],each=2)
# [1] "a" "a" "c" "c" "f" "f" "h" "h" "k" "k" "m" "m" "p" "p" "r" "r" "u" "u" "w" "w" "z" "z"
lttrs_TF=index_TF(letters,[True,False,True,False,False])
input=numpy.repeat(lttrs_TF, 2)
rd=list(input)
print(rd)

rep_times_TF(letters, times = 1, each = 2,index=[True,False,True,False,False])

#Example 6
#> rep(letters[c(TRUE,FALSE,TRUE,FALSE,FALSE)],each=2,len=25)
#[1] "a" "a" "c" "c" "f" "f" "h" "h" "k" "k" "m" "m" "p" "p" "r" "r" "u" "u" "w" "w" "z" "z"
#[23] "a" "a" "c"
lttrs_TF=index_TF(letters,[True,False,True,False,False])
input=itertools.cycle(numpy.repeat(lttrs_TF, 2))
output=list(itertools.islice(itertools.cycle(input), 25))
print(output)



rep_lengthout_TF(letters, lengthout=25 , each = 2,index=[True,False,True,False,False])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM