I would like to repeat elements from one list based in a second list, like this:
JavaScript
x
8
1
i = 0
2
j = [1, 4, 10]
3
z = [11.65, 11.69, 11.71]
4
for x in j:
5
while i <= x:
6
print(x)
7
i += 1
8
I’ve got this result:
JavaScript
1
12
12
1
1
2
1
3
4
4
4
5
4
6
10
7
10
8
10
9
10
10
10
11
10
12
I’d like to get this result:
JavaScript
1
12
12
1
11.65
2
11.65
3
11.69
4
11.69
5
11.69
6
11.71
7
11.71
8
11.71
9
11.71
10
11.71
11
11.71
12
Advertisement
Answer
You may iterate on both list together, using zip
, then increase i
until you reach the bound of the current value
JavaScript
1
8
1
i = 0
2
j = [1, 4, 10]
3
z = [11.65, 11.69, 11.71]
4
for bound, value in zip(j, z):
5
while i <= bound:
6
print(value)
7
i += 1
8