On Friday 03 July 2009 15:37, Emile van Sebille wrote: > On 7/3/2009 1:21 PM Kent Johnson said... > > > On Fri, Jul 3, 2009 at 3:49 PM, Dinesh B > > > > Vadhia<[email protected]> wrote: > >> d = [0, 8, 4, 4, 4, 7, 2, 5, 1, 1, 5, 11, 11, 1, 6, 3, 5, 6, 11, 1] > >> > >> and we want: > >> > >> [0, 8, 12, 16, 20, 27, 29, 34, 35, 36, 41, 52, 63, 64, 70, 73, 78, 84, > >> 95, 96] > >> dd = [ sum(d[:j]) for j in range(len(d)) ][1:] > >> > >> gives: > >> > >> [0, 8, 12, 16, 20, 27, 29, 34, 35, 36, 41, 52, 63, 64, 70, 73, 78, 84, > >> 95] > > > > In [9]: [ sum(d[:j+1]) for j in range(len(d)) ] > > Out[9]: [0, 8, 12, 16, 20, 27, 29, 34, 35, 36, 41, 52, 63, 64, 70, 73, > > 78, 84, 95, 96] > > So, did we get an 'A'... > > Emile > > _______________________________________________ > Tutor maillist - [email protected] > http://mail.python.org/mailman/listinfo/tutor
The problem with these list comprehensions is that they have O(n**2) complexity. Whether they are faster or not depends on the speedup from the list comprehension and the length of the input. I'd be inclined to favor the linear for loop. Cheers _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
