Split a string into a list according to the specified format

I have a type string "SAB_bARGS_D". I want the string to be divided by a list of characters, but whenever there is a _ sign, the next character is added to the previous one.

So the answer above should be ['S','A','B_b','A','R','G','S_D']

This can be done using a for loop that goes through the list, but there is a built-in function that I can use .....

Thank you so much


Refresh

Hello everybody

Thanks Robert Rossney, aaronasterlingI got the required answer, but I have exactly the same question that I’m going to ask here only ... Lets say that now my line has criticism, that it can have a letter or a letter followed by _ and a number ..... How can I separate the line in the list now ... The proposed solutions cannot be used now, since S_10 will be divided into S_1 and 0 ...... It would be useful if someone could say how do it using RE .... thanks a lot ....

+3
source share
3 answers

I know I will use regular expressions:

>>> import re
>>> pattern = "[^_]_[^_]|[^_]"
>>> re.findall(pattern, "SAB_bARGS_D", re.IGNORECASE)
['S', 'A', 'B_b', 'A', 'R', 'G', 'S_D']

The pattern tries to match 3 characters per line — not underscore, underline, not underline — and, otherwise, tries to match a character without underscore.

+5

, , for.

def a_split(inp_string):
    res = []
    if not inp_string: return res  # allows us to assume the string is nonempty

    # This avoids taking res[-1] when res is empty if the string starts with _
    # and simplifies the loop.
    inp = iter(inp_string)   
    last = next(inp)
    res.append(last)

    for c in inp:
        if '_' in (c, last): # might want to use (c == '_' or last == '_')
            res[-1] += c
        else:
            res.append(c)
        last = c
    return res

res.append res, , append.

'a_b_c', . , , - . '_ab' ['_a', 'b'] 'ab_'.

+2

Using regex

>>> import re
>>> s="SAB_bARGS_D"
>>> re.findall("(.(?:_.)?)",s)
['S', 'A', 'B_b', 'A', 'R', 'G', 'S_D']
+1
source

Source: https://habr.com/ru/post/1781650/


All Articles