Question

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

Was it helpful?

Solution 2

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)

OTHER TIPS

You want the part directly preceding the last dot; just split on the dots and take the one-but last part:

for line in data:
    if not '.' in line: continue
    elem = line.strip().split('.')[-2]

For your input, that gives:

>>> for line in data:
...     print line.strip().split('.')[-2]
... 
y
x
x
y
co
s
t

I would follow this logic:

For each line:

  • remove any spaces at beginning and end
  • split the line by dots
  • take the part before last of the splitted list

This should give you the part of the sentence you're looking for.

Simply use the split function.

a = 'x.com'
b = a.split('.')

This will make a list of 2 items in b. If you have two dots, the list will contain 3 items. The function actually splits the string based on the given character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top