Re: string methods (warning, newbie)

2005-02-28 Thread TZOTZIOY
On Sun, 27 Feb 2005 18:12:17 -0700, rumours say that Steven Bethard
<[EMAIL PROTECTED]> might have written:

[snip Nick Coghlan's list comprehension]

[STeVe]
>On the other hand, filter doesn't do the same thing:
>
>py> s = u'The Beatles - help - 03 - Ticket to ride'
>py> filter(str.isalpha, s)
>Traceback (most recent call last):
>   File "", line 1, in ?
>TypeError: descriptor 'isalpha' requires a 'str' object but received a 
>'unicode'
>py> ''.join(c for c in s if c.isalpha())
>u'TheBeatleshelpTickettoride'

This works though:

.>> filter(type(s).isalpha, s)

-- 
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string methods (warning, newbie)

2005-02-28 Thread TZOTZIOY
On Sun, 27 Feb 2005 18:12:17 -0700, rumours say that Steven Bethard
<[EMAIL PROTECTED]> might have written:

[snip Nick Coghlan's list comprehension]

[STeVe]
>On the other hand, filter doesn't do the same thing:
>
>py> s = u'The Beatles - help - 03 - Ticket to ride'
>py> filter(str.isalpha, s)
>Traceback (most recent call last):
>   File "", line 1, in ?
>TypeError: descriptor 'isalpha' requires a 'str' object but received a 
>'unicode'
>py> ''.join(c for c in s if c.isalpha())
>u'TheBeatleshelpTickettoride'

This works though:

.>> filter(type(s).isalpha, s)

As a function just for clarity.
-- 
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string methods (warning, newbie)

2005-02-27 Thread Steven Bethard
Nick Coghlan wrote:
Jimmy Retzlaff wrote:
The approach you are considering may be easier than you think:
filter(str.isalpha, 'The Beatles - help - 03 - Ticket to ride')
'TheBeatleshelpTickettoride'
Hmm, I think this is a case where filter is significantly clearer than 
the equivalent list comprehension:

Py> "".join([c for c in 'The Beatles - help - 03 - Ticket to ride' if 
c.isalpha(
)])
'TheBeatleshelpTickettoride'
On the other hand, filter doesn't do the same thing:
py> s = u'The Beatles - help - 03 - Ticket to ride'
py> filter(str.isalpha, s)
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: descriptor 'isalpha' requires a 'str' object but received a 
'unicode'
py> ''.join(c for c in s if c.isalpha())
u'TheBeatleshelpTickettoride'

Ideally, you could use something like basestring.isalpha and have it 
work for both str and unicode, but no such luck. =)

STeVe
--
http://mail.python.org/mailman/listinfo/python-list


Re: string methods (warning, newbie)

2005-02-27 Thread anthonyberet
Jimmy Retzlaff wrote:
Anthonyberet wrote:
Is there a string mething to return only the alpha characters of a
string?
eg 'The Beatles - help - 03 - Ticket to ride', would be
'TheBeatlesTickettoride'
If not then how best to approach this?
I have some complicated plan to cut the string into individual
characters and then concatenate a new string with the ones that return
true with the .isalpha string method.
Is there an easier way?

The approach you are considering may be easier than you think:

filter(str.isalpha, 'The Beatles - help - 03 - Ticket to ride')
'TheBeatleshelpTickettoride'
Thanks very much - that's the level of knowledge of Python that I just 
don't have yet - everything I try to do seems to have a much easier way, 
that I haven't encountered yet :)

I shall read up on the elements of your code to understand exactly what 
it is doing.
--
http://mail.python.org/mailman/listinfo/python-list


Re: string methods (warning, newbie)

2005-02-26 Thread Nick Coghlan
Jimmy Retzlaff wrote:
The approach you are considering may be easier than you think:

filter(str.isalpha, 'The Beatles - help - 03 - Ticket to ride')
'TheBeatleshelpTickettoride'
Hmm, I think this is a case where filter is significantly clearer than the 
equivalent list comprehension:

Py> "".join([c for c in 'The Beatles - help - 03 - Ticket to ride' if c.isalpha(
)])
'TheBeatleshelpTickettoride'
Py>
Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: string methods (warning, newbie)

2005-02-26 Thread Terry Reedy

"anthonyberet" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Is there a string mething to return only the alpha characters of a 
> string?
> eg 'The Beatles - help - 03 - Ticket to ride', would be 
> 'TheBeatlesTickettoride'

I believe you can do this with string.translate (string module, not str())

tjr



-- 
http://mail.python.org/mailman/listinfo/python-list


RE: string methods (warning, newbie)

2005-02-26 Thread Jimmy Retzlaff
Anthonyberet wrote:
> Is there a string mething to return only the alpha characters of a
string?
> eg 'The Beatles - help - 03 - Ticket to ride', would be
> 'TheBeatlesTickettoride'
> 
> If not then how best to approach this?
> I have some complicated plan to cut the string into individual
> characters and then concatenate a new string with the ones that return
> true with the .isalpha string method.
> 
> Is there an easier way?

The approach you are considering may be easier than you think:

>>> filter(str.isalpha, 'The Beatles - help - 03 - Ticket to ride')
'TheBeatleshelpTickettoride'

Jimmy
--
http://mail.python.org/mailman/listinfo/python-list


Re: string methods (warning, newbie)

2005-02-26 Thread Peter Hansen
anthonyberet wrote:
Is there a string mething to return only the alpha characters of a string?
eg 'The Beatles - help - 03 - Ticket to ride', would be 
'TheBeatlesTickettoride'

If not then how best to approach this?
I have some complicated plan to cut the string into individual 
characters and then concatenate a new string with the ones that return 
true with the .isalpha string method.

Is there an easier way?
Look into the string module's "translate" function.  It will
do this for you fairly easily.
-Peter
--
http://mail.python.org/mailman/listinfo/python-list


Re: string methods (warning, newbie)

2005-02-26 Thread anthonyberet
anthonyberet wrote:
Is there a string mething [method] to return only the alpha characters of a string?
eg 'The Beatles - help - 03 - Ticket to ride', would be 
'TheBeatlesTickettoride' 

erm, no it wouldn't, it would be 'TheBeatleshelpTickettoride', but you 
get me, I am sure.

If not then how best to approach this?
I have some complicated plan to cut the string into individual 
characters and then concatenate a new string with the ones that return 
true with the .isalpha string method.

Is there an easier way?
--
http://mail.python.org/mailman/listinfo/python-list