gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:08:39 24 4
gpt4 key购买 nike

美好的一天。

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

(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)

关于python - 通过 +-*/() (作为分隔符)分割线而不在 python 中省略它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26611382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com