在python中重新创建句子:列表索引超出范围
我试着用组成句子的单词和单词的位置来写一个句子。当我运行代码时,会出现错误“列表索引超出范围”。我似乎不知道出了什么问题。我将非常感谢任何帮助。谢谢:)。下面是我所有的代码在python中重新创建句子:列表索引超出范围,python,Python,我试着用组成句子的单词和单词的位置来写一个句子。当我运行代码时,会出现错误“列表索引超出范围”。我似乎不知道出了什么问题。我将非常感谢任何帮助。谢谢:)。下面是我所有的代码 def compress(): #function that will compress the inputted sentence/sentences sentence = input("Input the sentence that you wish to be compressed")
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) 在recreate函数中,当在compress函数中将单词写入tsk3wds.txt时,可以查找文件words.txt
2) 在第17-18行中,当您实际需要列表单词中该单词的索引时,您正在将该单词的索引保存在未压缩的句子中。您可以将这两行更改为这一行,并且最有可能工作:
index = [index for index, x in enumerate(wds) if x == j][0]
d[j] = index
plist.append(index)
你在哪一行得到错误?向我们显示堆栈跟踪;-)@谢谢你的回复。我在第74、72、69、36、71和54行有错误。请提供所有错误的详细信息。@Matthew Cliatt给出的错误是“索引器:列表索引超出范围”您刚才说在第74、72、69、36和54行有错误。它们都是相同的错误吗?谢谢你的回答。当我把它放到代码中时,它返回了一个答案,并说索引没有定义。回溯(最近一次调用last):文件“E:\code\Task 3_8.py”,第18行,在d[j]=索引名错误:名称“index”未定义>>>我们只是在这里定义索引,所以应该定义它。可能是缩进错误。这三行是否在else语句中?这是else语句:else:index=[index for index,x in enumerate(wds)if x==j][0]d[j]=index plist.append(index)现在它说plist不是definedIt应该可以工作,但是由于某种原因它不能工作。我在你的问题中直接编辑了代码,你能试试吗?按原样复制/粘贴?