假設 yyy 的長度是跟著 ttt 的話
kkk = []
ttt = [111,222,333]
yyy = [1,4,8]
for i in range(len(ttt)):
for j in range(yyy[i]):
kkk.append(ttt[i])
print(kkk)
# [111, 222, 222, 222, 222, 333, 333, 333, 333, 333, 333, 333, 333]
如果說沒有一定要直接去修改 kkk 的陣列的話:
import itertools
kkk = list(itertools.chain.from_iterable(itertools.repeat(n, count) for n, count in zip(ttt, yyy)))
也可以使用 zip
kkk = []
ttt = [111, 222, 333]
yyy = [1, 4, 8]
for t, y in zip(ttt, yyy):
kkk.extend([t] * y)
print(kkk)
# [111, 222, 222, 222, 222, 333, 333, 333, 333, 333, 333, 333, 333]