Re: Remove empty strings from list

2009-09-16 Thread Bruno Desthuilliers
Sion Arrowsmith a écrit : Bruno Desthuilliers wrote: mylist = line.strip().split() will already do the RightThing(tm). So will mylist = line.split() Yeps, it's at least the second time someone reminds me that the call to str.strip is just useless here... Pity my poor old neuron :( -- h

Re: Remove empty strings from list

2009-09-15 Thread Rhodri James
On Tue, 15 Sep 2009 02:55:13 +0100, Chris Rebert wrote: On Mon, Sep 14, 2009 at 6:49 PM, Helvin wrote: Hi, Sorry I did not want to bother the group, but I really do not understand this seeming trivial problem. I am reading from a textfile, where each line has 2 values, with spaces before and

Re: Remove empty strings from list

2009-09-15 Thread Sion Arrowsmith
Bruno Desthuilliers wrote: > >> mylist = line.strip().split() > >will already do the RightThing(tm). So will mylist = line.split() -- \S under construction -- http://mail.python.org/mailman/listinfo/python-list

Re: Remove empty strings from list

2009-09-15 Thread Bruno Desthuilliers
Dennis Lee Bieber a écrit : (snip) All of which can be condensed into a simple for ln in f: wrds = ln.strip() # do something with the words -- no whitespace to be seen I assume you meant: wrds = ln.strip().split() ?-) -- http://

Re: Remove empty strings from list

2009-09-15 Thread Bruno Desthuilliers
Dave Angel a écrit : (snip) As Chris says, you're modifying the list while you're iterating through it, and that's undefined behavior. Why not do the following? mylist = line.strip().split(' ') mylist = [item for item in mylist if item] Mmmm... because the second line is plain useless when

Re: Remove empty strings from list

2009-09-15 Thread Bruno Desthuilliers
Helvin a écrit : Hi, Sorry I did not want to bother the group, but I really do not understand this seeming trivial problem. I am reading from a textfile, where each line has 2 values, with spaces before and between the values. I would like to read in these values, but of course, I don't want the

Re: Remove empty strings from list

2009-09-14 Thread Join hack
good solution ,thanks~! 2009/9/15 Steven D'Aprano > On Mon, 14 Sep 2009 18:55:13 -0700, Chris Rebert wrote: > > > On Mon, Sep 14, 2009 at 6:49 PM, Helvin wrote: > ... > > > I have looked at documentation, and how strings and lists work, but I > > > cannot understand the behaviour of the followi

Re: Remove empty strings from list

2009-09-14 Thread Steven D'Aprano
On Mon, 14 Sep 2009 18:55:13 -0700, Chris Rebert wrote: > On Mon, Sep 14, 2009 at 6:49 PM, Helvin wrote: ... > > I have looked at documentation, and how strings and lists work, but I > > cannot understand the behaviour of the following: ... > >                        for item in list: > >        

Re: Remove empty strings from list

2009-09-14 Thread Gabriel Genellina
En Mon, 14 Sep 2009 23:33:05 -0300, tec escribió: or use filter list=filter(lambda x: len(x)>0, list) For strings, len(x)>0 <=> len(x) <=> x, so the above statement is equivalent to: list=filter(lambda x: x, list) which according to the documentation is the same as: list=filter(None, li

Re: Remove empty strings from list

2009-09-14 Thread Dave Angel
Helvin wrote: Hi, Sorry I did not want to bother the group, but I really do not understand this seeming trivial problem. I am reading from a textfile, where each line has 2 values, with spaces before and between the values. I would like to read in these values, but of course, I don't want the wh

Re: Remove empty strings from list

2009-09-14 Thread Helvin Lui
Thanks Chris! Thanks for the quick reply. Indeed this is the case! I have now written out a new list, instead of modifying the list I am iterating over. Logged at my blog: http://learnwithhelvin.blogspot.com/2009/09/python-loop-and-modify-list.html Regards, Helvin =) On Tue, Sep 15, 2009 at 1:55

Re: Remove empty strings from list

2009-09-14 Thread tec
Helvin 写道: Hi, Sorry I did not want to bother the group, but I really do not understand this seeming trivial problem. I am reading from a textfile, where each line has 2 values, with spaces before and between the values. I would like to read in these values, but of course, I don't want the white

Re: Remove empty strings from list

2009-09-14 Thread tec
Chris Rebert 写道: On Mon, Sep 14, 2009 at 6:49 PM, Helvin wrote: Hi, Sorry I did not want to bother the group, but I really do not understand this seeming trivial problem. I am reading from a textfile, where each line has 2 values, with spaces before and between the values. I would like to read

Re: Remove empty strings from list

2009-09-14 Thread Chris Rebert
On Mon, Sep 14, 2009 at 6:49 PM, Helvin wrote: > Hi, > > Sorry I did not want to bother the group, but I really do not > understand this seeming trivial problem. > I am reading from a textfile, where each line has 2 values, with > spaces before and between the values. > I would like to read in the