Re: [Tutor] list comprehension problem

2009-07-03 Thread bob gailer
Dinesh B Vadhia wrote: I'm suffering from brain failure (or most likely just being brain less!) and need help to create a list comprehension for this problem: d is a list of integers: d = [0, 8, 4, 4, 4, 7, 2, 5, 1, 1, 5, 11, 11, 1, 6, 3, 5, 6, 11, 1] Want to create a new list that adds the

Re: [Tutor] list comprehension problem

2009-07-03 Thread Emile van Sebille
On 7/3/2009 4:19 PM Emile van Sebille said... On 7/3/2009 3:54 PM Dinesh B Vadhia said... As the lists of integers get larger (mine are in the thousands of integers per list) the list comprehension solution will get slower. Do you agree? Yes, no doubt. Your original post asked only if the

Re: [Tutor] list comprehension problem

2009-07-03 Thread Emile van Sebille
On 7/3/2009 3:54 PM Dinesh B Vadhia said... Thanks Emile / Kent. The problem I see with this solution is that at each stage it is re-summing the j's instead of retaining a running total which the 'for-loop' method does ie. >>> dd = [] >>> y = d[0] >>> for i, x in enumerate(d): >>>

Re: [Tutor] list comprehension problem

2009-07-03 Thread Alan Gauld
"Dinesh B Vadhia" wrote As the lists of integers get larger ... the list comprehension solution will get slower. Do you agree? Yes thats why Chris said the linear loop solution is almost certainly faster in this case. However you could speed up the for loop significantly by missing out th

Re: [Tutor] list comprehension problem

2009-07-03 Thread Dinesh B Vadhia
y += x >>>dd.append(y) As the lists of integers get larger (mine are in the thousands of integers per list) the list comprehension solution will get slower. Do you agree? Dinesh From: Kent Johnson Sent: Friday, July 03, 2009 1:21 PM To: Dinesh B Vadhia Cc: tutor@python.o

Re: [Tutor] list comprehension problem

2009-07-03 Thread Emile van Sebille
On 7/3/2009 1:59 PM Chris Fuller said... The problem with these list comprehensions is that they have O(n**2) complexity. But, the more you work with them the more ease you'll develop at understanding and deploying them. I often find that a (quick) (perhaps complex) list comprehension is

Re: [Tutor] list comprehension problem

2009-07-03 Thread Chris Fuller
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 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,

Re: [Tutor] list comprehension problem

2009-07-03 Thread Emile van Sebille
On 7/3/2009 1:21 PM Kent Johnson said... On Fri, Jul 3, 2009 at 3:49 PM, Dinesh B Vadhia 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

Re: [Tutor] list comprehension problem

2009-07-03 Thread Kent Johnson
On Fri, Jul 3, 2009 at 3:49 PM, Dinesh B Vadhia 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, 1

Re: [Tutor] list comprehension problem

2009-07-03 Thread Dinesh B Vadhia
] Dinesh Message: 6 Date: Fri, 03 Jul 2009 12:22:30 -0700 From: Emile van Sebille To: tutor@python.org Subject: Re: [Tutor] list comprehension problem Message-ID: Content-Type: text/plain; charset=ISO-8859-1; format

Re: [Tutor] list comprehension problem

2009-07-03 Thread Emile van Sebille
On 7/3/2009 12:09 PM Dinesh B Vadhia said... I'm suffering from brain failure (or most likely just being brain less!) and need help to create a list comprehension for this problem: d is a list of integers: d = [0, 8, 4, 4, 4, 7, 2, 5, 1, 1, 5, 11, 11, 1, 6, 3, 5, 6, 11, 1] Want to create a n

[Tutor] list comprehension problem

2009-07-03 Thread Dinesh B Vadhia
I'm suffering from brain failure (or most likely just being brain less!) and need help to create a list comprehension for this problem: d is a list of integers: d = [0, 8, 4, 4, 4, 7, 2, 5, 1, 1, 5, 11, 11, 1, 6, 3, 5, 6, 11, 1] Want to create a new list that adds the current number and the pri