Need simple algorithm for newbie

Gerhard Häring gerhard.haering at opus-gmbh.net
Mon Nov 4 07:45:10 EST 2002


Jason Tudisco <tudisco at sexmagnet.com> [2002-11-04 12:33 GMT]:
> I have a list of domains... Some of the domain names in the list look
> like this:
> 
> groups.goodle.com
> 
> The information I want is just google.com. I need to know the best way
> to do this.. for .com .net .org only.. and to strip the rest of the
> garbage.. like in this case.. get rid of groups in groups.google.com
> 
> I need to parse though a huge list so it has to be optimized algorithm

I did a little experimentation in the interactive interpreter:

C:\>python
Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = "groups.google.com"
>>> x.split(".")
['groups', 'google', 'com']
>>> x.split(".")[-2:]
['google', 'com']
>>> ".".join(x.split(".")[-2:])
'google.com'

The [-2:] slicing might not be obvious at first - it slices out the last two
elements of the list.

-- Gerhard



More information about the Python-list mailing list