【问题标题】:Seperate string character without function python [duplicate]没有函数python的单独字符串字符[重复]
【发布时间】:2020-12-02 15:23:29
【问题描述】:

你能把这个“+2x-5y+8+2y”分开吗(字符之间没有空格) 像 python 中的 [+2x,-5y,+8,+2y] ?怎么样?

【问题讨论】:

标签: python string character slice


【解决方案1】:

如果您知道所有可能进入的输入模式,请查看regular expressions

在您的情况下,解决方案可能类似于

import re
a = "+2x-5y+8+2y"
print(re.findall(r"[+-]\d[xy]?", a))

re.findall(expression, input) 需要一个正则表达式和一个应该被解析的参数 在上述解决方案中,模式[+-]\d[xy]?

  • [+-] + 或 - 符号
  • \d 任何数字,如果您希望使用多个数字,请改用 \d+(+ 在这里表示“至少重复一次”)
  • [xy]? 字符 x 和 y 之一,但它可能会丢失。如果您还期望其他字母,请将它们添加到这些括号内

【讨论】:

    猜你喜欢
    • 2016-01-17
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2012-10-16
    • 2012-12-13
    相关资源
    最近更新 更多