Reputation: 2441
Let v = [10:1, 15:2, 20:3]; (i = [0, 1, 2] the indices of the values in the list)
n = 10 - (i + 1) = 10 - (0 + 1) = 9 ==> newV = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
n = 15 - (9 + i + 1) = 15 - (9 + 1 + 1) = 4 ==> newV =[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2]
n = 20 - ( 9 + 4 + 2 + 1 ) = 20 - 16 = 4 ==> newV =[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3]
finalV = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3]`
if __name__ == '__main__':
v = ['1:1', '15:2', '30:3']
previous_Idx = []
newV = []
for i in range(0, len(v)):
idx = int(v[i].split(':')[0])
val = float(v[i].split(':')[-1])
n = idx - (sum(previous_Idx) + i + 1)
for _ in range(n):
newV.append(0)
previous_Idx.append(n)
newV.append(val)
print newV
thank you pencil and paper.
Upvotes: 0
Views: 235
Reputation: 2109
there are some unclear points in your question but here is a possible solution
for line in open("file.txt", 'r').readlines():
line = line.strip().split(" ")
lineName = line.pop(0)
arr = [0.0] * int(line[-1].split(":")[0]) # fills the array with zeros
for element in line:
index,value = element.split(":")
arr[int(index)-1] = float(value)
print lineName + " " + " ".join(map(str, arr)) # /questions/6507431
Upvotes: 1