demand
Key and value are saved inkey_counts. Generate a list and generate value key based on the number of value.
question
finds that each time you loop, there is an extra null value at the end of the generated new list.
Code
key_counts = [{"key": "qj", "value": 3}, {"key": "wuhan", "value": 2}, {"key": "xy", "value": 2}, {"key": "zz", "value": 10}]
total = 17 -sharp
v1 = ["" for x in range(total)]
print(v1)
count = 0
for key_count in key_counts:
start = count
end = start + key_count["value"] - 1
count = end + 1
print(start, end, key_count["key"])
xlist = [key_count["key"] for x in range(key_count["value"])]
print(xlist)
v1[start:end] = xlist
print(v1)
output results
[",","]
0 2 qj
["qj"," qj", "qj"]
[" qj","", "",""]
34 wuhan
["wuhan"," wuhan"]
["qj"," wuhan", "wuhan","",
56 xy
["xy"," xy"]
["qj"," wuhan", "wuhan"," xy", "xy","]
7 16 zz
["zz"," zz", "zz"," zz"]
["qj"," wuhan", "wuhan"," xy", "xy"," zz",", ""]
list, with each loop, an additional element is added (which may be misunderstood. )