Repeat each values of an array different times

前端 未结 2 1347
一生所求
一生所求 2020-11-28 16:19

Suppose a = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] and s = [3, 3, 9, 3, 6, 3]. I\'m looking for the best way to repeat a[i] exactly s[i]

2条回答
  •  庸人自扰
    2020-11-28 16:57

    Here's a one-liner using only (nested) list comprehensions:

    [item for z in [[x]*y for (x,y) in zip(a, s)] for item in z]
    

提交回复
热议问题