R function rep() in Python (replicates elements of a list/vector)

后端 未结 8 1797
无人共我
无人共我 2021-01-31 14:20

The R function rep() replicates each element of a vector:

> rep(c(\"A\",\"B\"), times=2)
[1] \"A\" \"B\" \"A\" \"B\"

This is like the list m

8条回答
  •  无人共我
    2021-01-31 14:54

    Use numpy arrays and the numpy.repeat function:

    import numpy as np
    
    x = np.array(["A", "B"])
    print np.repeat(x, [2, 3], axis=0)
    
    ['A' 'A' 'B' 'B' 'B']
    

提交回复
热议问题