简体   繁体   English

Python“ IndexError:字符串索引超出范围”(初学者)

[英]Python “IndexError: string index out of range” (Beginner)

So for a Programming assignment we have to re write the sort function in python to sort a list of words. 因此,对于编程作业,我们必须在python中重新编写sort函数以对单词列表进行排序。 So far I have made it able to sort the words based of the first letter of each and now im trying to run recursion to still sort it if the first letters or any of the letters are the same. 到目前为止,我已经使它能够根据每个字母的第一个字母对单词进行排序,现在我尝试运行递归以在第一个字母或任何字母相同的情况下仍对它进行排序。 Im having problems with the "IndexError: string index out of range" error. 我在“ IndexError:字符串索引超出范围”错误时遇到问题。 What I have so far is 我到目前为止所拥有的是

def insertion_sort(bookwords):
    for index in range(1,len(bookwords)):
        global word
        word=bookwords[index]
        i=index-1
        word_checker(bookwords, 0, i)

def word_checker(bookwords, num, i):
    while i>=0:
        wordleft=bookwords[i]
        if ord(word[num])<ord(wordleft[num]):
            bookwords[i+1]=bookwords[i]
            bookwords[i]=word
            i=i-1
        elif ord(word[num])==ord(wordleft[num]):
            num=num+1
            word_checker(bookwords, num, i)
        else:
            break


bookwords=["michael", "maddy", "michelle", "monstor", "money", "mountain", "miniscus", "mega"]

insertion_sort(bookwords)

print bookwords

Im guessing num is becoming larger than the words but its running through many times without stopping when the letters arn't the same so im abit confused why its doing that. 我猜num正在变得比单词大,但是当字母不相同时,它会运行很多次而不会停止,所以我有点困惑为什么这样做。 Any help would be greatly appreciated 任何帮助将不胜感激

UPDATE UPDATE

Ok now it works but when I put it into the supplied code to test how fast it is with about 700000 words it went for 30+ until I stopped it where as the sort function took 5 seconds. 好的,现在可以工作了,但是当我将其放入提供的代码中以测试大约700000个单词的速度时,它运行了30多个,直到我将其停止为止,因为排序功能花了5秒钟。 Here is the code with my part as well 这也是我的一部分代码

import re
import pygame

# 159.172 assignment 2
# 
def mysort(words):
for index in range(1,len(words)):
    word=words[index]
    i=index-1
    word_checker(words, i, word)

def word_checker(words, i, word):
while i>=0:
    wordleft=words[i]
    if word==wordleft:
        break
    elif word<wordleft:
        words[i+1]=words[i]
        words[i]=word
        i=i-1
    else:
        return

# Do NOT change anything below here:
#
# Compare two lists
def compare(l1,l2):
    if len(l1) != len(l2):
        return False
    for a,b in zip(l1,l2):
        if a!=b:
            return False
    return True

# Open the book
book=open("allsherlock.txt", "rt")

# Make a list of all the words in the book
bookwords=[]
for line in book:
    for word in re.findall(r'\w+', line):
        bookwords.append(word.lower())

print "Loaded",len(bookwords),"words"
sortedbookwords=bookwords[:]
pygame.init()
# Use the sort function to sort the words for testing
sortedbookwords.sort()
starttime=pygame.time.get_ticks()
# Call our sort function
mysort(bookwords)
print "Sort took",pygame.time.get_ticks()-starttime,"ms"
print "Correct sort:",compare(bookwords,sortedbookwords)

you have to change this: 您必须更改此:

 elif ord(word[num])==ord(wordleft[num]):
     num=num+1
     word_checker(bookwords, num, i)
 else:

to: 至:

 elif ord(word[num])==ord(wordleft[num]):
     num=num+1
 else:

then it will print: ['maddy', 'mega', 'money', 'michael', 'michelle', 'miniscus', 'monstor', 'mountain'] 然后会打印: ['maddy', 'mega', 'money', 'michael', 'michelle', 'miniscus', 'monstor', 'mountain']

i don't see the point of doing recursion there anyway, i think insertion sort doesn't do recursion. 我仍然看不到在那儿进行递归的意义,我认为插入排序不会做递归。

update 更新

The algorithm was broken when comparing by character, but python can compare the strings for you, so this will give the right result: 按字符比较时,算法已损坏,但是python可以为您比较字符串,因此这将给出正确的结果:

def insertion_sort(bookwords):
    for index in range(1,len(bookwords)):
        global word
        word=bookwords[index]
        i=index-1
        word_checker(bookwords, i)

def word_checker(bookwords,  i):
    while i>=0:
        wordleft=bookwords[i]
        if word<wordleft:
            bookwords[i+1]=bookwords[i]
            bookwords[i]=word
        i=i-1

bookwords=["michael", "maddy", "michelle", "monstor", "money", "mountain", "miniscus", "mega"]
insertion_sort(bookwords)
print bookwords #prints ['maddy', 'mega', 'michael', 'michelle', 'miniscus', 'money', 'monstor', 'mountain']

Several things: 几件事:

  • Python zero-indexes strings (so from 0 to len(string)-1). Python对字符串进行零索引(因此从0到len(string)-1)。 and
  • Consider just using "for" to go through each letter. 考虑只使用“ for”来遍历每个字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM