简体   繁体   English

零序列

[英]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. 我需要在列表中的每个i上附加一个零序列(最大长度为5),并根据i[2]值将i[1]添加到该序列中。

Eg for i = 0 , the sequence of zeroes is 1,0,0,0,0 . 例如,对于i = 0 ,零序列为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 . 等于1i[1]已放置在最前面,因为i[2]值为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. 类似地,对于i = 3i[1] = 1所以序列为0,1,0,0,0因为i[2]值为2 => 1进入零序列的第二位。

In general, the i[1] value will go in the i[2] place in the sequence of zeroes. 通常, i[1]值将以零序列出现在i[2]位置。 If i[[1] = 8 and i[2] = 4 then sequence = 0,0,0,8,0 . 如果i[[1] = 8i[2] = 4则序列= 0,0,0,8,0 Each of these sequences will need to appended onto the ith array. 这些序列中的每一个都需要附加到第ith个数组上。

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 快乐的解析

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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