![](/img/trans.png)
[英]How to count special characters, Alphabets and numbers from a string in 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',無]
+ 或 - 符號總是在數字之后。
您可以執行此類操作,這適用於您提供的所有輸入。 不是最 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 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+', ';']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.