简体   繁体   中英

Sequence of zeroes

I have a list like

[[1,1,1],[2,1,1],[2,1,2],[2,5,3],[2,4,4],[2,3,5],...]

I need to append a sequence of zeroes (max length is five) onto each i in my list and add i[1] into that sequence depending on the i[2] value.

Eg for i = 0 , the sequence of zeroes is 1,0,0,0,0 . The i[1] which is equal to 1 has been placed at the front because the i[2] value is 1 . Similarly, for i = 3 , i[1] = 1 so the sequence is 0,1,0,0,0 as the i[2] value is 2 => 1 goes in the 2nd place of the sequence of zeroes.

In general, the i[1] value will go in the i[2] place in the sequence of zeroes. If i[[1] = 8 and i[2] = 4 then sequence = 0,0,0,8,0 . Each of these sequences will need to appended onto the ith array.

My desired output is:

[[1,1,0,0,0,0],[2,1,0,0,0,0],[2,0,1,0,0,0],[2,0,0,5,0,0],[2,0,0,0,4,0],[2,0,0,0,0,5],...]

I'm not sure if this makes sense but please do ask for more clarification.

this is one of the awful questions where you ask people to find a solution for dealing with some badly designed data structure. but today is Saturday and I have fun with oneliners:

d=[[1,1,1],[2,1,1],[2,1,2],[2,5,3],[2,4,4],[2,3,5]]

left = [[x[0]]+[0]*(x[2]-1)+[x[1]] for x in d]
print [l + [0]*(5-len(l)) for l in left]

happy parsing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM