Skip to main content
 首页 » 编程设计

python之通过 +-*/() (作为分隔符)分割线而不在 python 中省略它们

2025年05月04日10Leo_wl

美好的一天。

我正在尝试拆分(标记化)一个字符串,例如

(22+33)* 44/300

并得到类似的输出

['(','22','+','33',')','*','44','/','300']

到目前为止我尝试使用

infix = input("Enter the infix notation : ") 
infix = re.split(r'[+-/*()]', infix) 

但它省略了分隔符并在列表中创建了 '' 元素。

请您参考如下方法:

re.split

infix = input("Enter the infix notation : ") 
infix = re.split(r'([+-/*()])', infix)