【问题标题】:How would I divide a string after a specific character in Pythonpython - 如何在Python中的特定字符之后划分字符串
【发布时间】:2021-07-08 07:03:05
【问题描述】:

编辑:感谢您对模棱两可的问题和结构不佳的示例做出如此多的回应和道歉。我并不是要按大写字符来拆分单词。我的字面意思是在“X”、“r”或“o”等特定字符之后拆分字符串。

我正在尝试将一长串重复的单词(例如“FooXXBarBazXBar ...”)拆分为单词列表 ['Fo', 'o', 'X', 'X', 'Bar', 'Baz '、'X'、'条形图' ...]。

在 c 中,我会创建一个新的字符数组并一个接一个地复制字符,在特定字符之后插入一个分隔符,以便稍后在分隔符上拆分,如下所示:

{
    char *newString = malloc(sizeof(char)*200);
    int offset=0;

    //copy characters from old string to the new one, inserting ';' after a specific character

    for (int i = 0, n = strlen(string); i < n; i++)
    {
        newString[i+offset] = string[i];
        if ((string[i] == 'o') || (string[i] == 'r') || (string[i] == 'z') || (string[i] == 'X'))
        {
            offset += 1;
            newString[i+offset] = ';';
        }
    }

    //split the string into an array of strings on the delimiter ';'
    split_string(newString);
}

我还是一个初学者,所以它可能也不是那么好,但它确实有效。

我正在尝试在 python 中解决这个问题,但无法提出一个好的方法。

你会怎么做?谢谢:)

【问题讨论】:

  • 我认为您必须更好地说明拆分如何工作的问题,例如为什么要拆分 Foo 而不是 FooXFooXX 。我认为您想拆分字母的重复模式(从最长的模式开始),所以Foo 要在字符串中的某处拆分,应该有另一个Foo 模式,等等。
  • 还有为什么您使用关键字Python 而您的代码sn-p 在C 中?
  • @BrunoVermeulen 也许结果词必须以 [aeiou] 结尾?
  • 您好,谢谢您的回答。我的 sn-p 在 C 中,因为我知道如何实现它。我使用了 Python 关键字,因为我正在寻找 Python 中这个问题的解决方案。我在显示输入“Foo”和输出 [“Foo”] 的示例中犯了一个错误。在我正在寻找的内容中,它会给出 ["Fo", "o"] 我想在我的代码中定义一个特定字符并在该字符之后拆分字符串。

标签: python string split


【解决方案1】:

如果我理解正确,并且您想要做的是将每个单词拆分为大写字母(“HelloWorldPython”-> [“Hello”,“World”,“Python”]

我会使用正则表达式:

import re
text = "FooXXBarBazXBar"
re.findall(r'[A-Z][^A-Z]*', text) # Uppercase letter followed by 0 or more non-uppercased character
['Foo', 'X', 'X', 'Bar', 'Baz', 'X', 'Bar']

【讨论】:

    【解决方案2】:

    你的问题有点模棱两可。目前尚不清楚您要在哪些字符处拆分字符串,但如果这些字符是 ['o','r','X','z'] 那么以下代码将执行以下操作:

    string = input()
    word = ''
    words_list = []
    for char in string: # iterate through the string
        word += char # add the character to the word
        if(char in ['o','r','X','z']): # check if the character at current iteration is one of the characters in the list
            words_list.append(word) # add the word formed by now to the list
            word = '' # re-initialize the word variable to an empty string so as to start the next word
    print(words_list)
    

    这将输出:

    ['Fo', 'o', 'X', 'X', 'Bar', 'Baz', 'X', 'Bar', 'xxX']
    
    警告:由于您已将 'o' 指定为拆分字符,它永远不会给您 'Foo' 以便在列表中获取 'Foo' 而不是 'Fo',您必须在循环中添加更多代码。

    【讨论】:

    • 太棒了,谢谢!这正是我想要的!我在示例中选择了单词“foo”并没有注意到其中有两个 'o' 时犯了一个错误?
    • 欢迎您! @Nishijama 错误并没有错,事实上这些都很漂亮。
    【解决方案3】:

    将字符串拆分为以大写字母开头的部分。
    就像将 "ThisIsALineAsAnExample" 拆分为 ['This','Is','A','Line','As','An','Example']

    如果这是您想要做的,那么这可能会有所帮助

    i=0
    j=1
    final=[]
    s="ThisIsALineAsAnExample"
    while(i<len(s)):
        if(s[i].isupper()):
            new_str=s[i]
            j=i+1
            while(j<len(s) and s[j].islower()):
                new_str=new_str+s[j]
                j=j+1
            print(new_str)
        i=j
        final.append(new_str)
    
    print(final)
    

    【讨论】:

      【解决方案4】:

      示例输入和输出表明您要拆分以大写字母开头的每个单词。

      对于 Python,使用这种方法的解决方案 can be found here

      如果您也想将它应用到您的 C 解决方案中,您可以通过以下方式检查字符是否为大写:

      if(string[i] >= 'A' && string[i] <= 'Z') {
          offset += 1;
          newString[i+offset] = ';';
      }
      

      【讨论】:

        猜你喜欢
        • 2020-06-17
        • 2021-03-05
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        • 2019-04-22
        • 1970-01-01
        • 1970-01-01
        • 2021-11-05
        相关资源
        最近更新 更多