在python中重新创建句子:列表索引超出范围

时间:2016-04-04 22:42:22

标签: python

我正在尝试使用组成句子的单词和单词的positiosn来重新创建一个句子。当我运行我的代码时,我得到错误'列表索引超出范围'。我似乎无法弄清楚出了什么问题。我非常感谢任何帮助。谢谢 :)。我的所有代码都在下面。

        def compress():     #function that will compress the inputted sentence/sentences

    sentence = input("Input the sentence that you wish to be compressed") #Sentence to be compressed
    sentence.lower()#Puts sentence in lower-case
    sentencelist = sentence.split() #Splits the sentence into a list
    d = {} #Dictionary

    plist = [] #List that contains the positions of the words
    wds = []
    for i in sentencelist: #iterating through the inputted sentence
        if i not in wds: #if item not in list of words...
            wds.append(i) #...append to the list of words
    for i ,j in enumerate(sentencelist):#Enumerates the sentence and gets the positions
        if j in (d): #if the item (j) is in the d 
            plist.append(d[j]) #append the item to the list of positions
        else:
            d[j] =i             #else, append item (i) to position list
            plist.append(i)     #appends to the list of positions 
    print (plist) #print the list containing the positions. 


    with open ("tsk3pos.txt", "wt") as txt: #opens the file

        position_string = " ".join(str(x) for x in plist)   #makes/recreates sentence using positons and words
        txt.write(position_string)

        txt.close()                     #closes the file
        with open ("tsk3wds.txt", "wt") as txt: #opens the file

            for item in wds:            #iterates through list of words 
                txt.write("%s\n" % item)    #puts lists in the file
        txt.close() #closes the file


    print (wds) #prints list that contains words that are in the sentence
    main()  #calls main function



def recreate(compress): #function that will be used to recreate the compressed sentence.

    num = []    #creates list for positions (blank)
    wds = []    #creates list for words (blank)

    with open("words.txt", "r") as txt: #with statement opening the word text file
        for line in txt: #iterating over each line in the text file.
            wds += line.split() #turning the textfile into a list and appending it to num

    with open("tsk3pos.txt", "r") as txt:   #opens text file with list of positions in read code
        for line in txt:                    #iterates through
            num += [int(i) for i in line.split()]    #turns the textfile into list and appends to blank list


    recreate = ' '.join(wds[pos] for pos in num)    #makes/recreates sentence using positons and words

    with open("recreate.txt", "wt") as txt: #opens recreate text file in write mode
        txt.write(recreate)                 #writes sentences to 'recreate' text file

    main()                                  #calls the  main function



def main():                                 #defines main function
    print("Do you want to compress an input or recreate a compressed input?") #user input 
    user = input("Type 'a' if you want to compress an input. Type 'b' if you wan to recreate an input").lower() #gives user choice ad puts input in lower case
    if user not in ("a","b"):                   #if input isn't a or b...
        print ("That's not an option. Please try again")    #give error message
    elif user == "a":           #if input is a...
        compress()              #...call the compress function
    elif user == "b":           #if input is b...
        recreate(compress)      #...call recreate function with compress as argument
    main()                      #calls main function

main()                          #Calls main function

1 个答案:

答案 0 :(得分:0)

我看到两个错误:

1)在你的重新创建功能中,你在压缩函数中查找文件words.txt,你将这些单词写入tsk3wds.txt

2)在第17-18行中,当您实际需要列表wds中该单词的索引时,您将保存未压缩句子中单词的索引。您可以将这两行更改为此行,并且很可能会有效:

index = [index for index, x in enumerate(wds) if x == j][0]
d[j] = index
plist.append(index)