There is a list consisting of line elements, which, in turn, consist of letters and a dash. Like that:

['---atcggctacgt--tactgcatgtca------', 'tc---------agtacgtactactgacgtca---', 'gcgtatagcgttga--------cgtgacgtacgg'] 

The task: to make the dashes at the beginning and at the end of line elements be replaced by dots, and the dashes standing in the middle of the lines remain unchanged. At the same time, you also need to get a list. Like this:

 ['...atcggctacgt--tactgcatgtca......', 'tc---------agtacgtactactgacgtca...', 'gcgtatagcgttga--------cgtgacgtacgg'] 

I believe that you have to convert each item into a separate list. But in general, I do not know how to solve this problem. I would be very grateful for the decision.

    3 answers 3

    Well, if you have fun, and do not use regulars, you get something like this:

     def dash_dot(s): n = len(s) return '.' * (n - len(s.lstrip('-'))) + s.strip('-') + '.' * (n - len(s.rstrip('-'))) x = ['---atcggctacgt--tactgcatgtca------', 'tc---------agtacgtactactgacgtca---', 'gcgtatagcgttga--------cgtgacgtacgg'] print([dash_dot(s) for s in x]) 

    PS Oh ... a line from some minuses will work incorrectly, if it occurs, you must put a separate condition on s.strip('-') == ''

      You can find the indices in the first and last letter, and everything that is before the first letter and after the last (by condition, except for a dash there is nothing), you can replace with dots using this function:

       def replace_on_borders(string): first_letter_idx = len(string) last_letter_idx = 0 for idx, letter in enumerate(string): if letter.isalpha(): if idx > last_letter_idx: last_letter_idx = idx if idx < first_letter_idx: first_letter_idx = idx return ''.join(['.' * (first_letter_idx), string[first_letter_idx:last_letter_idx+1], '.' * (len(string) - last_letter_idx - 1)]) 

      A new list can be created using the list generator:

       old_strings = [ '---atcggctacgt--tactgcatgtca------', 'tc---------agtacgtactactgacgtca---', 'gcgtatagcgttga--------cgtgacgtacgg' ] new_strings = [replace_on_borders(string) for string in old_strings] 

      The output of such commands

       import pprint pprint=pprint.PrettyPrinter(indent=4).pprint pprint(new_strings) 

      It turns out the following:

       [ '...atcggctacgt--tactgcatgtca......', 'tc---------agtacgtactactgacgtca...', 'gcgtatagcgttga--------cgtgacgtacgg'] 

      that is, like, everything, as it should

        The problem can be solved with the use of regular expressions, making a replacement by a pattern.

        We use the following patterns: (^-*) - hyphens at the beginning of a line, (-*$) - at the end of a line.

        In the replacement function, we get the length of the coincidence group, return the corresponding sequence of points.

         import re ls = '---atcggctacgt--tactgcatgtca------',\ 'tc---------agtacgtactactgacgtca---',\ 'gcgtatagcgttga--------cgtgacgtacgg' def repl(match): return '.' * len(match.group()) patt = re.compile(r'(^-*)|(-*$)') for i in ls: print(patt.sub(repl, i)) # ...atcggctacgt--tactgcatgtca...... # tc---------agtacgtactactgacgtca... # gcgtatagcgttga--------cgtgacgtacgg 

        List formation:

         dotted_lst = [patt.sub(repl, i) for i in ls]