【问题标题】:Python split() replicationPython split() 复制
【发布时间】:2020-07-13 19:12:03
【问题描述】:

我目前正在尝试复制拆分功能。我环顾四周,发现一个有很大帮助的,代码的唯一问题是空字符串没有显示为空,而是它们有多个撇号用于多个不同的空字符串。当前代码是

def mysplit(strng):

    words = []
    current_word = " "
    for char in strng:
        if char == " ":
            words.append(current_word)
            current_word = ""
        else:
            current_word += char

    words.append(current_word)
    return words


    print(mysplit("To be or not to be, that is the question"))
    print(mysplit("To be or not to be,that is the question"))
    print(mysplit("   "))
    print(mysplit(" abc "))
    print(mysplit(""))

输出的结果

[' To', 'be', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question']
[' To', 'be', 'or', 'not', 'to', 'be,that', 'is', 'the', 'question']
[' ', '', '', '']
[' ', 'abc', '']
[' ']

我正在尝试得到结果

['To', 'be', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question']
['To', 'be', 'or', 'not', 'to', 'be,that', 'is', 'the', 'question']
[]
['abc']
[]

【问题讨论】:

  • 您需要使用空字符串而不是空格来初始化current_word。然后,如果您只在 current_word 不为空时追加到words,您应该获得您正在寻找的内容(这并不是 split() 的工作方式)

标签: python


【解决方案1】:
def mysplit(strng):
    words = []
    if(len(strng)== 0 or strng.isspace()):
        return words
        
    current_word = " "
    for char in strng:
        if char == " ":
                words.append(current_word)
                current_word = ""
        else:
            
            current_word += char

    
    words.append(current_word)
    for w in words:
        if(w.isspace() or len(w)==0):
            words.remove(w) 
    
    return words


print(mysplit("To be or not to be, that is the question"))
print(mysplit("To be or not to be,that is the question"))
print(mysplit("   "))
print(mysplit(" abc "))
print(mysplit(""))

【讨论】:

    【解决方案2】:

    首先,您应该在返回单词之前删除 words.append(current_word)。

    然后你可以添加一个 if 使用 strip 函数来删除字符串前面和结尾的空格(https://www.journaldev.com/23625/python-trim-string-rstrip-lstrip-strip

    if char is " ":
        need_to_append, stripped_word = should_append(current_word)
        if need_to_append:
            words.append(stripped_word)
        current_word = ""
    

    最终版本:

    def should_append(word):
        stripped_word = word.strip()
        return stripped_word != "", stripped_word
    
    def mysplit(strng):
        words = []
        current_word = " "
        for char in strng:
            if char is " ":
                need_to_append, stripped_word = should_append(current_word)
                if need_to_append:
                    words.append(stripped_word)
                current_word = ""
             else:
                current_word += char
        need_to_append, stripped_word = should_append(current_word)
        if need_to_append:
            words.append(stripped_word)
        return words
    

    【讨论】:

    • 这比我以前唯一的问题要接近得多,它是如何将字符串中的最后一个单词留在外面的。
    • 哦,我明白了,我已经用新函数 should_append() 更新了我的答案,以了解我们是否需要添加单词。
    【解决方案3】:

    您有几个问题。请查看我的解决方案

    def mysplit(strng):
    
        words = []
        current_word = ""
        for char in strng:
            if char == " ":
                if current_word:
                    words.append(current_word)
                current_word = ""
            else:
                current_word += char
    
        if current_word:
            words.append(current_word)
        return words
    
    
    print(mysplit("To be or not to be, that is the question"))
    print(mysplit("To be or not to be,that is the question"))
    print(mysplit("   "))
    print(mysplit(" abc "))
    print(mysplit(""))
    

    【讨论】:

      【解决方案4】:

      你可以使用 enumerate 和 zip 来实现:

      def mysplit(string):
          spaces = [ i for i,c in enumerate(string) if c==" " ] # indexes of spaces
          subs   = [ string[s+1:e] for s,e in zip([-1]+spaces,spaces+[len(string)]) ]
          return [ s for s in subs if s ] # filter out empty strings
      

      【讨论】:

        【解决方案5】:

        当您找到分隔符(空格)时,需要检查是否有要添加的正确的非空白词。

            if char == " ":
                words.append(current_word)
                current_word = ""
        

        应该是

            if char == " ":
                if current_word:    # If there is anything to append, do so.
                    words.append(current_word)
                current_word = ""
        

        【讨论】:

        • 不适用于测试用例print(mysplit(" "))
        • 您可能需要在循环结束后添加最后一个单词时进行相同的检查。这样,如果输入以空格结尾,您就不会搞砸。
        • 对;这个答案是改变的关键;边界条件“留给学生练习”。
        猜你喜欢
        • 2018-12-30
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 2011-11-22
        • 1970-01-01
        相关资源
        最近更新 更多