0

I need to create a list of size N from already given list.

N = 3
a_list = [10,4,18,2,6,19,24,1,20]

The O/P should be:

[10,4,18] [4,18,2] [18,2,6] [2,6,19] [6,19,24] [19,24,1] [24,1,20]

It's like a window size of N=3 , which slides one step to its right.

How will I do it?

2
  • no, that will be to get [10,4,18],[2,6,19],[24,1,20] Commented Dec 6, 2013 at 6:43
  • 1
    @alko - I checked this thread already. But the problem was different Commented Dec 6, 2013 at 6:50

3 Answers 3

5

A faster way:

>>> zip(a_list,a_list[1:],a_list[2:])
[(10, 4, 18), (4, 18, 2), (18, 2, 6), (2, 6, 19), (6, 19, 24), (19, 24, 1), (24, 1, 20)]

Comparison:

In [6]: %timeit [a_list[i:i+n] for i in xrange(len(a_list)-n+1)]
100000 loops, best of 3: 9.61 us per loop

In [7]: %timeit zip(a_list,a_list[1:],a_list[2:])
100000 loops, best of 3: 5.23 us per loop

Or more general:

>>> zip(*[a_list[i:] for i in range(3)]) #3 (or 2, 4, 5, etc)is the length of step

For larger list, probably you have to use numpy to get a faster solution than @Ashwini Chaudhary's (http://www.rigtorp.se/2011/01/01/rolling-statistics-numpy.html):

import numpy as np
lista=np.array(lis)
def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

%timeit [lis[i:i+n] for i in xrange(len(lis)-n+1)] 
%timeit rolling_window(lista, n)

1 loops, best of 3: 171 ms per loop
100000 loops, best of 3: 19.7 µs per loop
Sign up to request clarification or add additional context in comments.

8 Comments

Please explain. how will zip(*[a_list[i:] for i in range(3)]) be valid for n=5
Oh, change 3 to any number, like 5. That is essentially zip(a_list,a_list[1:],a_list[2:],.....)
Now I've a string in my file as 10 4 18 2 6 19 24 1 20 . I'm able to read that string but how should I convert this 'Line' to an List?
how about map(int,s[:-1].split(' ')), s is your line 10 4 18 2 6 19 24 1 20\n, because when you readline() there is a \n in the end.
@Ashwini Chaudhary. That's interesting. Can't keep wondering about why it is so. For large dimension, probably there is no better way than yours except using numpy, see edit. Cheers.
|
5

Use list comprehension and slicing:

>>> lis = [10,4,18,2,6,19,24,1,20]
>>> n = 3
>>> [lis[i:i+n] for i in xrange(len(lis)-n+1)]
[[10, 4, 18], [4, 18, 2], [18, 2, 6], [2, 6, 19], [6, 19, 24], [19, 24, 1], [24, 1, 20]]
>>> n = 4
>>> [lis[i:i+n] for i in xrange(len(lis)-n+1)]
[[10, 4, 18, 2], [4, 18, 2, 6], [18, 2, 6, 19], [2, 6, 19, 24], [6, 19, 24, 1], [19, 24, 1, 20]]

For a bigger list the zip based approach is actually slower:

In [27]: n = 100                                                                                 

In [28]: lis = [10,4,18,2,6,19,24,1,20]*10000                                                    

In [30]: %timeit zip(*[lis[i:] for i in xrange(n)])                                              
1 loops, best of 3: 593 ms per loop                                                              

In [31]: %timeit [lis[i:i+n] for i in xrange(len(lis)-n+1)]                                      
10 loops, best of 3: 114 ms per loop    

5 Comments

Dang, i was slightly late. +1 sir.
Thanks. I was actually also actually thinking of using slicing, but was not clear on my thoughts. Thanks.
Now I've a string in my file as 10 4 18 2 6 19 24 1 20 . I'm able to read that string but how should I convert this 'Line' to an List?
What I'm doing is list((f1.readline()).replace(" ","")). it returns me ['1', '0', '4', '1', '8', '2', '6', '1', '9', '2', '4', '1', '2', '0', '\n']
@user1162512 lis = '10 4 18 2 6 19 24 1 20'.split()
0

try this

>>> lis = [10,4,18,2,6,19,24,1,20]
>>> n=3
>>> [lis[i:i+n] for i in range(len(lis))][:-2]

the output is

[[10, 4, 18], [4, 18, 2], [18, 2, 6], [2, 6, 19], [6, 19, 24], [19, 24, 1], [24, 1, 20]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.