Sunday, 15 January 2012

python - How do I repeatedly shift and pad elements in a list to get a list of lists? -


I have a list a = [1, 2, 3, ..., n] Want to change the list frequently and to get the list of more lists? The first line should be A , second line [2, 3, 4, ...] , third line [3, 4, 5,. ..] , [n, 0, 0, ...] to the last line. In the previous column the missing element must be zero. I was trying to install them individually, but n => 100 so that it will take a long time to manually padding zero. How do i do this

Edit The only question I have for embedded arrays, which I actually have.

  & gt; & Gt; & Gt; A = [1, 2, 3, 4]> gt; & Gt; & Gt; I range for [i:] + i * [0] in [[1, 2, 3, 4], [2, 3, 4, 0], [3, 4, 0]] (lane (a))] I am in 0], [4, 0, 0, 0]]  

how it works

to get the list transferred i'th , We can use it: one [i:] + i * [0] . The understanding of the list is that it does so often for all of us.

Use of the numpy

+ This means that the normal python lists for oval arrays. As a result, the above code needs to be copied with tweaks:

  & gt; & Gt; & Gt; Np & gt; & Gt; & Gt; A = np.arange (1, 5) & gt; & Gt; & Gt; Array ([4, 0, 0]), array ([1, 2, 3, 4]), array ([2, 3, 4, 0]), array ([3, 4, 0, 0] 0])]  

If you want the result to be the final result:

  & gt; & Gt; & Gt; Np.array (for [np.concatenate ((a [i:], np.zeros (i))) in range (lane (a)))) array ([[1., 2., 3., 4.], [2., 3., 4, 0.], [3., 4., 0., 0.], [4., 0., 0., 0.]])  

No comments:

Post a Comment