从 python 中的字符串中分离数字、特殊字符和 + 或 - 符号
Separating the numbers, special characters and + or - sign from a string in python
所以,我有一个像
这样的字符串
a = ';1'
b = '2+3'
c = '32'
d = '12-'
e = '2+;'
f = '2'
我想将它们分开以获得以下结果:
a: [';', '1']
b: ['2+', '3']
c: ['3', '2']
d: ['1', '2-']
e: ['2+', ';']
`f: ['2', None]
+ 或 - 号总是在数字后面。
你可以做这样的事情,这对你提供的所有输入都有效。这不是最 pythonic 的方法,但它有效。
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)
使用内置 itertools 模块的 pairwise()
和 chain()
:
# 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+', ';']]
所以,我有一个像
这样的字符串a = ';1'
b = '2+3'
c = '32'
d = '12-'
e = '2+;'
f = '2'
我想将它们分开以获得以下结果:
a: [';', '1']
b: ['2+', '3']
c: ['3', '2']
d: ['1', '2-']
e: ['2+', ';']
`f: ['2', None]
+ 或 - 号总是在数字后面。
你可以做这样的事情,这对你提供的所有输入都有效。这不是最 pythonic 的方法,但它有效。
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)
使用内置 itertools 模块的 pairwise()
和 chain()
:
# 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+', ';']]