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-12 Thread Stefan Behnel
Aahz, 12.05.2010 17:33: Stefan Behnel wrote: superpollo, 11.05.2010 17:03: Aahz ha scritto: In article, Terry Reedy wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Ho wrote: Have I missed something, or wouldn't this work just as well: list_of_str

Re: Extract all words that begin with x

2010-05-12 Thread Aahz
In article , Stefan Behnel wrote: >superpollo, 11.05.2010 17:03: >> Aahz ha scritto: >>> In article , >>> Terry Reedy wrote: On 5/10/2010 5:35 AM, James Mills wrote: > On Mon, May 10, 2010 at 6:50 PM, Xavier Ho wrote: >> Have I missed something, or wouldn't this work just as well: >

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 , Terry Reedy wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Ho wrote: Have I missed something, or wouldn't this work just as well: list_of_strings = ['2', 'awes',

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 , Terry Reedy wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Ho wrote: Have I missed something, or wouldn't this work just as well: list_of_strings = ['2', 'awes', '3465sdg', 'dbsdf', 'asdgas

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 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 er

Re: Extract all words that begin with x

2010-05-11 Thread Aahz
In article , Terry Reedy 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 added enough to >more than balance the slight expansion of the l

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 slic

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 met

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, wrote: word[len(word)-1:] This works just as well: word[-1:] d'uh. ;-) -- 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, 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 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 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 Jerry Hill
On Tue, May 11, 2010 at 10:37 AM, 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 sequence instead 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 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 ex

Re: Extract all words that begin with x

2010-05-11 Thread superpollo
Aahz ha scritto: In article , Terry Reedy wrote: On 5/10/2010 5:35 AM, James Mills wrote: On Mon, May 10, 2010 at 6:50 PM, Xavier Ho 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_

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 Aahz
In article , Terry Reedy wrote: >On 5/10/2010 5:35 AM, James Mills wrote: >> On Mon, May 10, 2010 at 6:50 PM, Xavier Ho 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_o

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 wrote: > On 5/10/2010 5:35 AM, James Mills wrote: >> >> On Mon, May 10, 2010 at 6:50 PM, Xavier Ho  wrote: >>> >>> Have I missed something, or wouldn't this work just as well: >>> >> list_of_strings = ['2', 'awes', '3465sdg', 'dbsdf', 'asdgas'] >>>

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 Ho 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 wo

Re: Extract all words that begin with x

2010-05-10 Thread Aahz
In article , James Mills wrote: >On Mon, May 10, 2010 at 6:50 PM, Xavier Ho 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

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 James Mills
On Mon, May 10, 2010 at 6:50 PM, Xavier Ho 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 completeness (ju

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 look

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 wrote: > Hello > > I am trying to find

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 func