Re: [Tutor] Working with lists - why does my script not work?

2012-06-25 Thread ALAN GAULD
CCing the list, please use replyAll when responding to the list.   By the way for some reason, the google class says that the functions sorted() is better than the method list.sort(). > > > >" list.sort() -- sorts the list in place (does not return it). (The sorted() >function shown below is pref

Re: [Tutor] Working with lists - why does my script not work?

2012-06-25 Thread Alan Gauld
On 25/06/12 14:39, Puneeth Chaganti wrote: `sorted`[0] returns a new list unlike `sort`[1], which sorts a list in-place. Something like this, would work: words = sorted(words) x_list = sorted(x_list) or just words.sort() x_list.sort() which will work in place as mentioned above. -- Alan G

Re: [Tutor] Working with lists - why does my script not work?

2012-06-25 Thread Puneeth Chaganti
On Mon, Jun 25, 2012 at 6:51 PM, Developer Ecofunds wrote: > Hello guys, > > I'd like to ask you a little question about a script I did and isn't working > properly. > It is one excercise from googles python classes > > > I'm u

Re: [Tutor] Working with lists - why does my script not work?

2012-06-25 Thread Kwpolska
On Mon, Jun 25, 2012 at 3:21 PM, Developer Ecofunds wrote: > I'm using python 2.5 because it's the one works with Google App Engine > Not quite. > Note: If you are starting a new project, it is recommended that you use the >

Re: [Tutor] Working with lists - why does my script not work?

2012-06-25 Thread Craig Cesareo
Try putting.[:] After "words" in the for loop line. What's happening is you're removing words from the list you are iterating through and it's messing with the loop. The empty indice I'm having you insert makes an in place copy of the list so your remove doesn't effect it. -- Craig Ces

[Tutor] Working with lists - why does my script not work?

2012-06-25 Thread Developer Ecofunds
Hello guys, I'd like to ask you a little question about a script I did and isn't working properly. It is one excercise from googles python classes < http://code.google.com/edu/languages/google-python-class/set-up.html> I'm using python 2.5 because it's the one works with Google App Engine < https

Re: [Tutor] Working with lists

2008-12-14 Thread Rich Lovely
Yeah, you could do that, but it was quite a revelation when I discovered itertools, and I'm just trying to share the love. --- Richard "Roadie Rich" Lovely Part of the JNP|UK Famille www.theJNP.com (Sent from my iPod - please allow me a few typos: it's a very small keyboard) On 14 Dec 2008

Re: [Tutor] Working with lists

2008-12-14 Thread bob gailer
Paul McGuire wrote: Even simpler than Rich Lovely's: newlist = [a+b for a,b in itertools.izip(l1[:-1], l1[1:])] is to just use the built-in zip: newlist = [a+b for a,b in zip(l1[:-1], l1[1:])] And then there's good old reduce which sadly is going to be harder to access in Python

Re: [Tutor] Working with lists

2008-12-14 Thread Paul McGuire
Even simpler than Rich Lovely's: newlist = [a+b for a,b in itertools.izip(l1[:-1], l1[1:])] is to just use the built-in zip: newlist = [a+b for a,b in zip(l1[:-1], l1[1:])] since you can be sure that l1[:-1] and l1[1:] will always be the same length, so there is no need for a fill valu

Re: [Tutor] Working with lists

2008-12-13 Thread Mark Tolonen
wrote in message news:20081213095244.n4clmwk3k4gkg...@webmail4.isis.unc.edu... Hi everyone, I seem to use this pattern alot when writing functions and I'm wondering if there is a more efficient method. It comes up whenever I want to work with more than one item in a list; for instance, say

Re: [Tutor] Working with lists

2008-12-13 Thread Alan Gauld
wrote Ideally, I'd be able to write: for num in list1: newlist.append(num+nextnum) This doesn't work, though because there's no way to access "nextnum" unless I implement a "count" variable like this: Or use an iterator: L = [1,2,3,4,5,6] i = iter(L) L2 = [n+i.next() for n in i] L2 [3,

Re: [Tutor] Working with lists

2008-12-13 Thread Richard Lovely
When you need indexes for a list you're iterating over, you want to look at the function enumerate(), which returns an iterator yielding (key, value) tuples. If you're working with multiple lists, itertools.imap and itertools.imap_longest are handy if you want to keep the lists in lockstep: >>> l

[Tutor] Working with lists

2008-12-13 Thread btkuhn
Hi everyone, I seem to use this pattern alot when writing functions and I'm wondering if there is a more efficient method. It comes up whenever I want to work with more than one item in a list; for instance, say I want to add each consecutive number in alist and output the new list. Ideally,