Need simple algorithm for newbie
sismex01 at hebmex.com
sismex01 at hebmex.com
Tue Nov 5 16:46:57 EST 2002
> From: Brad Hards [mailto:bhards at bigpond.net.au]
>
> ['groups', 'goodle', 'com', '']
> >>> a[-2:]
> ['com', '']
>
> The C way would be to test for a trailing "." and remove it
> if present. You could do that in python too, of course.
>
> I wondered if there was a better "more pythonic" way, knowing
> that we don't want any empty entries from the list?
>
> Brad
>
I don't know if it's really Pythonic, but it's fast and simple:
>>> S = 'groups.doodle.com.'
>>> S
'groups.doodle.com.'
>>> S.split('.')
['groups', 'doodle', 'com', '']
>>> filter(None, S.split('.'))
['groups', 'doodle', 'com']
>>> filter(None, S.split('.'))[-2:]
['doodle', 'com']
what 'filter(None, sequence)' does is return a new sequence with
all the elements of the original sequence which evaluate as true
(boolean). So, since 0, None, '', 0.0, [], {}, all evaluate as
false, they wouldn't be included in the final sequence.
-gustavo
More information about the Python-list
mailing list