【问题标题】:Python Splitting Line for AlphaNumeric Substrings Only仅用于 AlphaNumeric 子字符串的 Python 分割线
【发布时间】:2020-04-17 21:25:21
【问题描述】:

我想将一行拆分成它的子字符串,但我不想包含分号。我怎样才能做到这一点并忽略所有分号?

例如,如果字符串是这样的:

ignore the last semicolon;
ignore this last semicolon too ; (yes the space is intentional)

我想返回类似的东西:

['ignore','the','last','semicolon'] or
['ignore','the','last','semicolon;'] or
['ignore the last semicolon',';']

有什么建议吗?

【问题讨论】:

  • 请分享你到目前为止做了什么。它不是一个家庭作业解决门户。 :)
  • 您列出了 3 种不同的要退货的物品。你到底想要哪一个?
  • 我完全不清楚你想要什么;你给出了三个完全不同的结果。 any 如何“忽略”分号?第一个只是删除分号,您可以使用replace 处理它。另外两个给出明显不同的结果。第一个是split的默认操作;第二个可以使用regex。请发布您的需求和给您带来麻烦的编码尝试。
  • @NVSAbhilash 这不是功课。我正在自学 Python,并且一直在练习字符串操作,但这是我不知道该怎么做的一件事。
  • @Prune @izhang 如果我只是对句子进行拆分,它将返回['ignore','the','last','semicolon',';'],因为分号前可能有一个空格,我不希望它作为最后一个元素。跨度>

标签: python string split


【解决方案1】:

如果您的字符串是文本,您可以先使用 text_without_semicolon = text.replace(';', '') 然后使用 text_without_semicolon.split('') 这将为您提供所需的第一个输出。

【讨论】:

    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多