Python 在不使用split()字符串方法的情况下使用split函数。几乎

Python 在不使用split()字符串方法的情况下使用split函数。几乎,python,for-loop,split,Python,For Loop,Split,我正在尝试复制.split()字符串方法。它工作得很好,但不包括最后一句话 def stringSplitter(string): words = [] current_word = "" for x in range(len(string)): #problem is here if string[x] == " ": words.append(current_word) current_word = ""

我正在尝试复制.split()字符串方法。它工作得很好,但不包括最后一句话

def stringSplitter(string):
    words = []
    current_word = ""
    for x in range(len(string)): #problem is here
        if string[x] == " ":
            words.append(current_word)
            current_word = ""
        else:
            current_word += string[x]
    return words
测试1:当句子=我喜欢骑自行车时,我的代码错误地输出:

['I', 'like', 'to', 'ride', 'my']
我想要的结果是:

['I', 'like', 'to', 'ride', 'my', 'bicycle']

添加
单词。在从函数返回之前追加(当前单词)
。那是你“丢失”的单词。此外,不需要使用
范围
或任何索引<代码>用于字符串中的x:直接在字符上迭代。

在函数返回之前添加
单词。追加(当前单词)
。那是你“丢失”的单词。此外,不需要使用
范围
或任何索引<对于字符串中的x,code>直接在字符上迭代。

我通过@DYZ的第一个答案得到了它。非常感谢。显然,我跳过了最后一个单词,因为我需要在返回之前添加(下面)

words.append(current_word) 
我的代码:

def stringSplitter(string):
    words = []
    current_word = ""
    for char in string:
        if char == " ":
            words.append(current_word)
            current_word = ""
        else:
            current_word += char
    words.append(current_word)        
    return words

我是在@DYZ的第一个答案的帮助下得到的。非常感谢。显然,我跳过了最后一个单词,因为我需要在返回之前添加(下面)

words.append(current_word) 
我的代码:

def stringSplitter(string):
    words = []
    current_word = ""
    for char in string:
        if char == " ":
            words.append(current_word)
            current_word = ""
        else:
            current_word += char
    words.append(current_word)        
    return words

注意,这可以使用生成器函数更简洁地实现-如果您不介意稍微偏离“real”
str.split()
函数实现:

>>> def split(string, delimiter=' '):
    current_word = ''
    for char in string:
        if char == delimiter:
            yield current_word
            current_word = ''
        else:
            current_word += char
    yield current_word


>>> list(split('I like to ride my bicycle'))
['I', 'like', 'to', 'ride', 'my', 'bicycle']
>>> 
您甚至可以修改它以允许返回分隔符:

>>> def split(string, delimiter=' ', save_delimiter=False):
    current_word = ''
    for char in string:
        if char == delimiter:
            yield current_word
            if save_delimiter:
                yield char
            current_word = ''
        else:
            current_word += char
    yield current_word


>>> list(split('I like to ride my bicycle', save_delimiter=True))
['I', ' ', 'like', ' ', 'to', ' ', 'ride', ' ', 'my', ' ', 'bicycle']
>>> 

注意,这可以使用生成器函数更简洁地实现-如果您不介意稍微偏离“real”
str.split()
函数实现:

>>> def split(string, delimiter=' '):
    current_word = ''
    for char in string:
        if char == delimiter:
            yield current_word
            current_word = ''
        else:
            current_word += char
    yield current_word


>>> list(split('I like to ride my bicycle'))
['I', 'like', 'to', 'ride', 'my', 'bicycle']
>>> 
您甚至可以修改它以允许返回分隔符:

>>> def split(string, delimiter=' ', save_delimiter=False):
    current_word = ''
    for char in string:
        if char == delimiter:
            yield current_word
            if save_delimiter:
                yield char
            current_word = ''
        else:
            current_word += char
    yield current_word


>>> list(split('I like to ride my bicycle', save_delimiter=True))
['I', ' ', 'like', ' ', 'to', ' ', 'ride', ' ', 'my', ' ', 'bicycle']
>>> 

非常感谢。最后,这就是我错过的。谢谢!最后,这就是我所缺少的。如果在python for循环中使用索引,通常会出错。如果在python for循环中使用索引,通常会出错。