Search code examples
pythonpython-2.7distinguishedname

How to differentiate lines with one dot and two dot?


I want to extract a specific part of a sentence. My problem is that I have a list of sentences that each have different formats. For instance:

X.y.com
 x.no
 x.com
 y.com
 z.co.uk
 s.com
 b.t.com

how can I split these lines based on the number of dots they have? If I want the second part of the sentence with two dots and the first part of the sentences with one dot


Solution

  • To anwser your question you could use count to count the number of times the '.' appears and then do whatever you need.

    >>> 't.com'.count('.')
    1
    >>> 'x.t.com'.count('.')
    2
    

    You could use that in a loop:

    for s in string_list:
        dots = s.count('.')
        if dots == 1:
            # do something here
        elif dots == 2:
            # do something else
        else:
            # another piece of code
    

    More pythonic way to solve your problem:

    def test_function(s):
        """
            >>> test_function('b.t.com')
            't'
    
            >>> test_function('x.no')
            'x'
    
            >>> test_function('z')
            'z'
        """
        actions = {0: lambda x: x
                   1: lambda x: x.split('.')[0],
                   2: lambda x: x.split('.')[1]}
        return actions[s.count('.')](s)