slice notation as values?

bonono at gmail.com bonono at gmail.com
Mon Dec 12 05:20:54 EST 2005


Antoon Pardon wrote:
> Suppose I have a list with 10 000 elements and I want
> the sum of the first 100, the sum of the second 100 ...
>
> One way to do that would be:
>
> for i in xrange(0,10000,100):
>   sum(itertools.islice(lst, i, i+100))
>
> But itertools.islice would each time start from the begining
> of the list and iterate over i elements before giving 100
> elements to sum. Which would make this implementation O(n^2)
> instead of O(n).
Can you use iter for this situation ?

a=iter(lst)
for i in xrange(0,10000,100):
  sum(itertools.islice(a,100))




More information about the Python-list mailing list