简体   繁体   中英

Separating the numbers, special characters and + or - sign from a string in python

So, I have a strings like

a = ';1'

b = '2+3'

c = '32'

d = '12-'

e = '2+;'

f = '2'

I want to separate them to get the following results:

a: [';', '1']

b: ['2+', '3']

c: ['3', '2']

d: ['1', '2-']

e: ['2+', ';']

`f: ['2', None]

The + or - sign always come after the digit.

You can do something of this sort, this works fine for all the inputs you have provided. Not the most pythonic way to do it, but it works.

a = ';1'
b = '2+3'
c = '32'
d = '12-'
e = '2+;'

inputs = [a, b, c, d, e]
output = list()
for expr in inputs:
    i = 0
    string = str()
    li = list()
    while (i < len(expr)):
        if (expr[i] >= '0' and expr[i] <= '9') and i < len(expr) - 1:
            if expr[i + 1] == '+' or expr[i + 1] == '-':
                string += expr[i]
                string += expr[i + 1]
                li.append(string)
                i += 1
            else:
                li.append(expr[i])
        else:
            li.append(expr[i])
        i += 1
    output.append(li)

print(output)

Using pairwise() and chain() from the built-in itertools module:

# itertools.pairwise is available on python 3.10+, use this function on earlier versions
# copied from https://docs.python.org/3/library/itertools.html#itertools.pairwise
def pairwise(iterable):
    # pairwise('ABCDEFG') --> AB BC CD DE EF FG
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

def split(s: str) -> typing.List[str]:
    # iterate pairwise: "2+3" --> [("2", "+"), ("+", "3"), ("3", None)]
    # the None is just added to get the last number right
    pairs = pairwise(chain(s, [None]))
    output = []
    for l in pairs:
        if l[1] and l[1] in "+-":
            # number is followed by a "+" or "-" --> join together
            output.append("".join(l))
        elif l[0] not in "+-":
            # number is not followed by +/-, append the number
            output.append(l[0])
    return output

# checking your examples:
strings = [';1', '2+3', '32', '12-', '2+;']
[split(s) for s in strings]
# [[';', '1'], ['2+', '3'], ['3', '2'], ['1', '2-'], ['2+', ';']]

# bonus: everything in one line because why not
[[("".join(l) if l[1] and l[1] in "+-" else l[0]) for l in pairwise(chain(s, [None])) if l[0] not in "+-"] for s in strings]
# [[';', '1'], ['2+', '3'], ['3', '2'], ['1', '2-'], ['2+', ';']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM