Re: Extract all words that begin with x

2010-05-12 Thread Bryan
Terry Reedy wrote: Thank you for that timing report. Enjoyed doing it, and more on that below. My main point is that there are two ways to fetch a char, the difference being the error return -- exception IndexError versus error value ''. This is an example of out-of-band versus in-band

Re: Extract all words that begin with x

2010-05-12 Thread Stefan Behnel
Bryan, 12.05.2010 08:55: Now back to the arguably-interesting issue of speed in the particular problem here: 'Superpollo' had suggested another variant, which I appended to my timeit targets, resulting in: [s for s in strs if s.startswith('a')] took: 5.68393977159 [s for s in strs if s[:1] ==

Re: Extract all words that begin with x

2010-05-12 Thread Stefan Behnel
superpollo, 11.05.2010 17:03: Aahz ha scritto: In article mailman.11.1273548189.32709.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Hocont...@xavierho.com wrote: Have I missed something, or

Re: Extract all words that begin with x

2010-05-12 Thread superpollo
Stefan Behnel ha scritto: superpollo, 11.05.2010 17:03: Aahz ha scritto: In article mailman.11.1273548189.32709.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Hocont...@xavierho.com wrote: Have I

Re: Extract all words that begin with x

2010-05-12 Thread Aahz
In article mailman.100.1273653829.32709.python-l...@python.org, Stefan Behnel stefan...@behnel.de wrote: superpollo, 11.05.2010 17:03: Aahz ha scritto: In article mailman.11.1273548189.32709.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: On 5/10/2010 5:35 AM, James Mills wrote:

Re: Extract all words that begin with x

2010-05-12 Thread Stefan Behnel
Aahz, 12.05.2010 17:33: Stefan Behnelstefan...@behnel.de wrote: superpollo, 11.05.2010 17:03: Aahz ha scritto: In articlemailman.11.1273548189.32709.python-l...@python.org, Terry Reedytjre...@udel.edu wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier

Re: Extract all words that begin with x

2010-05-12 Thread Terry Reedy
On 5/12/2010 11:33 AM, Aahz wrote: also, what if the OP intended words that begin with x with x a string (as opposed to a single character) ? word[:len(x)] == x will work in that case. But that's now going to be slower. ;-) (Unless one makes the obvious optimization to hoist len(x)

Re: Extract all words that begin with x

2010-05-11 Thread Aahz
In article mailman.11.1273548189.32709.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Hocont...@xavierho.com wrote: Have I missed something, or wouldn't this work just as well: list_of_strings =

Slice last char from string without raising exception on empty string (Re: Extract all words that begin with x)

2010-05-11 Thread python
Terry, ... word[0:1] does the same thing. All Python programmers should learn to use slicing to extract a char from a string that might be empty. Is there an equivalent way to slice the last char from a string (similar to an .endswith) that doesn't raise an exception when a string is empty?

Re: Extract all words that begin with x

2010-05-11 Thread superpollo
Aahz ha scritto: In article mailman.11.1273548189.32709.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Hocont...@xavierho.com wrote: Have I missed something, or wouldn't this work just as well:

Re: Slice last char from string without raising exception on empty string (Re: Extract all words that begin with x)

2010-05-11 Thread superpollo
pyt...@bdurham.com ha scritto: Terry, ... word[0:1] does the same thing. All Python programmers should learn to use slicing to extract a char from a string that might be empty. Is there an equivalent way to slice the last char from a string (similar to an .endswith) that doesn't raise an

Re: Slice last char from string without raising exception on empty string (Re: Extract all words that begin with x)

2010-05-11 Thread Jerry Hill
On Tue, May 11, 2010 at 10:37 AM, pyt...@bdurham.com wrote: Is there an equivalent way to slice the last char from a string (similar to an .endswith) that doesn't raise an exception when a string is empty? If you use negative indexes in the slice, they refer to items from the end of the

Re: Slice last char from string without raising exception on empty string (Re: Extract all words that begin with x)

2010-05-11 Thread python
Superpollo, word[len(word)-1:] Perfect! Thank you, Malcolm -- http://mail.python.org/mailman/listinfo/python-list

Re: Slice last char from string without raising exception on empty string (Re: Extract all words that begin with x)

2010-05-11 Thread python
Jerry, If you use negative indexes in the slice, they refer to items from the end of the sequence instead of the front. So slicing the last character from the string would be: word[-1:] Perfect! Thank you, Malcolm -- http://mail.python.org/mailman/listinfo/python-list

Re: Slice last char from string without raising exception on empty string (Re: Extract all words that begin with x)

2010-05-11 Thread James Mills
On Wed, May 12, 2010 at 2:01 AM, pyt...@bdurham.com wrote: word[len(word)-1:] This works just as well: word[-1:] cheers James -- http://mail.python.org/mailman/listinfo/python-list

Re: Slice last char from string without raising exception on empty string (Re: Extract all words that begin with x)

2010-05-11 Thread superpollo
James Mills ha scritto: On Wed, May 12, 2010 at 2:01 AM, pyt...@bdurham.com wrote: word[len(word)-1:] This works just as well: word[-1:] d'uh. ;-) -- http://mail.python.org/mailman/listinfo/python-list

Re: Extract all words that begin with x

2010-05-11 Thread Bryan
Tycho Andersen wrote: Terry Reedy wrote:  ... word[0:1] does the same thing. All Python programmers should learn to use slicing to extract a  char from a string that might be empty. The method call of .startswith() will be slower, I am sure. Why? Isn't slicing just sugar for a method

Re: Extract all words that begin with x

2010-05-11 Thread Terry Reedy
On 5/11/2010 6:01 PM, Bryan wrote: Tycho Andersen wrote: Terry Reedy wrote: ... word[0:1] does the same thing. All Python programmers should learn to use slicing to extract a char from a string that might be empty. The method call of .startswith() will be slower, I am sure. Why? Isn't

Re: Extract all words that begin with x

2010-05-11 Thread Aahz
In article mailman.84.1273630878.32709.python-l...@python.org, Terry Reedy tjre...@udel.edu wrote: .startswith and .endswith are methods that wrap the special cases of slice at an end and compare to one value. There are not necessary, and save no keystrokes, but Guido obviously thought they

Extract all words that begin with x

2010-05-10 Thread Jimbo
Hello I am trying to find if there is a string OR list function that will search a list of strings for all the strings that start with 'a' return a new list containing all the strings that started with 'a'. I have had a search of Python site I could not find what I am looking for, does a

Re: Extract all words that begin with x

2010-05-10 Thread Xavier Ho
Have I missed something, or wouldn't this work just as well: list_of_strings = ['2', 'awes', '3465sdg', 'dbsdf', 'asdgas'] [word for word in list_of_strings if word[0] == 'a'] ['awes', 'asdgas'] Cheers, Xav On Mon, May 10, 2010 at 6:40 PM, Jimbo nill...@yahoo.com wrote: Hello I am trying

Re: Extract all words that begin with x

2010-05-10 Thread superpollo
Jimbo ha scritto: Hello I am trying to find if there is a string OR list function that will search a list of strings for all the strings that start with 'a' return a new list containing all the strings that started with 'a'. I have had a search of Python site I could not find what I am

Re: Extract all words that begin with x

2010-05-10 Thread James Mills
On Mon, May 10, 2010 at 6:50 PM, Xavier Ho cont...@xavierho.com wrote: Have I missed something, or wouldn't this work just as well: list_of_strings = ['2', 'awes', '3465sdg', 'dbsdf', 'asdgas'] [word for word in list_of_strings if word[0] == 'a'] ['awes', 'asdgas'] I would do this for

Re: Extract all words that begin with x

2010-05-10 Thread superpollo
superpollo ha scritto: Jimbo ha scritto: Hello I am trying to find if there is a string OR list function that will search a list of strings for all the strings that start with 'a' return a new list containing all the strings that started with 'a'. I have had a search of Python site I could

Re: Extract all words that begin with x

2010-05-10 Thread Aahz
In article mailman.2840.1273484131.23598.python-l...@python.org, James Mills prolo...@shortcircuit.net.au wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Ho cont...@xavierho.com wrote: Have I missed something, or wouldn't this work just as well: list_of_strings = ['2', 'awes', '3465sdg',

Re: Extract all words that begin with x

2010-05-10 Thread Terry Reedy
On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Hocont...@xavierho.com wrote: Have I missed something, or wouldn't this work just as well: list_of_strings = ['2', 'awes', '3465sdg', 'dbsdf', 'asdgas'] [word for word in list_of_strings if word[0] == 'a']

Re: Extract all words that begin with x

2010-05-10 Thread Tycho Andersen
On Mon, May 10, 2010 at 10:23 PM, Terry Reedy tjre...@udel.edu wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Hocont...@xavierho.com  wrote: Have I missed something, or wouldn't this work just as well: list_of_strings = ['2', 'awes', '3465sdg',