代码来模拟.split()函数在Python中的作用

时间:2015-12-14 17:45:41

标签: python python-3.x

我需要为学校项目制作一个程序。它必须执行.split()函数在Python中的功能。但我必须从头开始写它。

到目前为止,我无法在列表中添加任何超过一个空格的内容,我需要添加无限数量的字符串。如果我给它不止一个字符,它就不会离开循环。我也不允许使用break

sent = input("Give a sentence here:")

List=[]
Bpoint=0
Epoint=0
inString=[Bpoint,Epoint]

Epoint=+1
x=True
while x==True:
    if Epoint >= len(sent):
            x=False
    elif Epoint < len(sent):
        if sent[Epoint] == chr(32):
            List.append(inString[Bpoint:Epoint])
            Bpoint=Epoint + 1
            x=False
            if Epoint >= len(sent):
                x=False
            elif Epoint < len(sent):
                x=True
        elif sent[Epoint] != chr(32):
            if Epoint >= len(sent):
                x=False
            elif Epoint < len(sent):
                x=True
            Epoint=+1
        else:
            print ("Somethings wrong. Close and start over.")
print (List)

1 个答案:

答案 0 :(得分:3)

我将如何解决这个问题。

首先,重命名变量,整理风格:

sentence = input("Give a sentence here:")

results = []
word_start = 0
word_end = 0
in_string = [word_start, word_end]

word_end = 1
running = True

while running:
    if word_end >= len(sentence):
        running = False
        continue

    # If we get here, we know that `word_end < len(sentence)`
    if sentence[word_end] == " ":  # chr(32) was just confusing
        results.append(sentence[word_start:word_end])  # Note that using inString was wrong here
        word_start = word_end + 1
        running = False
        if word_end >= len(sentence):
            running = False
        else:  # We know `word_end < len(sent)` must be True
            running = True

    else:  # We know `sentence[word_end] != " "` must be True, no need to check
        if word_end >= len(sent):
            running = False
        else:  # No need for the elif check, because logic
            running = True
        word_end += 1  # Fixed typo error =+ vs +=
    # else: The 'something wrong' could never be reached. Either a character is a space, or not a space - there is no 3rd option!

print(results)

从结构的角度来看,代码大致没有变化,但至少现在它更容易看到发生了什么。下一步是开始修复结构。我注意到的第一件事是我们有单词开头和字结束计数器,我们需要手动维护它们。这有点难看,所以我们可以用句子上enumerate的for循环替换while循环。我还注意到in_string没有被正确使用,所以我们将摆脱它:

sentence = input("Give a sentence here:")

results = []
word_start = 0

for current_index, letter in enumerate(sentence):

    if sentence[current_index] == " ":
        # We've found a split between two words. See if we can add the previous word
        word = sentence[word_start:current_index]
        results.append(word)
        word_start = current_index + 1

        # Look at all the counter logic we've removed!

print(results)

所以现在我运行它,我发现它没有找到最后一个字:

Give a sentence here:Hi Tom this is a test
['Hi', 'Tom', 'this', 'is', 'a']

因此,在我们到达句子末尾之后,我们会退出循环,因为我们还没有空间来触发加词字码。我们可以通过输入&#34来证明这个理论。嗨Tom这是一个测试&#34; (注意最后一个空格):

Give a sentence here:Hi tom this is a test 
['Hi', 'tom', 'this', 'is', 'a', 'test']

好的,这就是问题,现在我们需要解决它。我会把它留给你:-)之后还有一些事情需要改进/修复(如果你输入"Hello world"会怎么样?),但是你需要亲自发现!祝你好运!