Need simple algorithm for newbie

Brad Hards bhards at bigpond.net.au
Tue Nov 5 16:28:18 EST 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, 6 Nov 2002 08:11, William Park wrote:
> I assume you only want the last 2 fields.  In that case,
>
> 1. Python:
>     a = 'groups.goodle.com'.split('.')
>     a[-2:]
I'm a python newbie too, but it occurs to me that this will be a problem for a 
FQDN, which someone will inevitably throw in just to be nasty:

>>> a = 'groups.goodle.com'.split('.')
>>> a
['groups', 'goodle', 'com']
>>> a[-2:]
['goodle', 'com']
>>> a = 'groups.goodle.com.'.split('.')
>>> a
['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

- -- 
http://linux.conf.au. 22-25Jan2003. Perth, Aust. I'm registered. Are you?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9yDfyW6pHgIdAuOMRAkSMAJ48Ib1yLD1v2vRsut2omQ2/KKxPZACePB4/
hvfZj8hvqA4ABCbjWR4AeHI=
=7rn8
-----END PGP SIGNATURE-----





More information about the Python-list mailing list