我有一个列表a = [3,7,4]
我想生成从列表的每个元素生成的序列的列表重复,如:
b = [1,1,1,2,2,2,2,2,2,2,3,3,3,3]
答案 0 :(得分:-1)
试试这样。
result = []
for i,j in enumerate(a):
result += [i+1 for n in range(j)]
<强>结果强>
[1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3]
答案 1 :(得分:-1)
输入:
a = [3,7,4]
b = [x for x in range(len(a)+1)[1::] for j in range(a[x-1])]
结果:
[1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3]