2016-04-04 9 views
-1

私は文章を構成する単語と単語のpositiosnを使ってセンテンスを再作成しようとしています。私のコードを実行すると、エラー 'List index of range'が出ます。何が間違っているのかわからない。私は大いに助けていただければ幸いです。ありがとう:)私のコードはすべて以下の通りです。Pythonで文を再作成する:リストのインデックスが範囲外にある

 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

どのラインでエラーが発生しますか?私たちにスタックトレースを示す;-) – Alfe

+0

@Alfe返信いただきありがとうございます。 74、72、69、36、71、54行目にエラーが表示されます。 – rmce

+0

すべてのエラーの詳細を入力してください。 –

答えて

0

私は2つのエラーを参照してください。あなたの圧縮機能であなたが

2をtsk3wds.txtする単語を書くときに、ファイルwords.txt探し

1)あなたの再作成機能での)で17〜18行目では、実際にリストwdsにその単語の索引が必要なときに、非圧縮文の単語のインデックスを保存しています。あなたはこれらの2行をこれに変更することができ、おそらく動作します:

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

ありがとうございます。これを私のコードに入れたら、答えが返ってきてインデックスが定義されていないと言いました。トレースバック(最新の呼び出しの最後): ファイル "E:\コード\タスク3_8.py"、ライン18、 D [J] =インデックス NameErrorで:名 'インデックス' が定義されていない >>> – rmce

+0

我々ここでインデックスを定義するだけなので、定義する必要があります。多分インデントエラーです。その3行はelse文の中にありますか? – fips

+0

これは、elseステートメントである:他: \t \t \tインデックス= [索引の索引、列挙中のx(WDS)は、x == jの場合] [0] \t \t \t D [J] =インデックス \t \t \t PLIST .append(index)これでplistが定義されていないと表示される – rmce

関連する問題