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

后端 未结 8 1779
无人共我
无人共我 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:34

    l = ['A','B']
    n = [2, 4]
    

    Your example uses strings which are already iterables. You can produce a result string which is similar to a list.

    ''.join([e * m for e, m in zip(l, n)])
    'AABBBB'
    

    Update: the list comprehension is not required here:

    ''.join(e * m for e, m in zip(l, n))
    'AABBBB'
    

提交回复
热议问题