简体   繁体   English

Python清单:索引超出范围

[英]Python List : Index out of Range

I've been trying to create a program that has to read in a file, find the unique words and punctuation, put those to a list and then get the positions of each word and store them in a list. 我一直在尝试创建一个程序,该程序必须读取文件,找到唯一的单词和标点,将它们放到列表中,然后获取每个单词的位置并将它们存储在列表中。 Then, using the lists the program will recreate the file. 然后,使用列表,程序将重新创建文件。 This is my code: 这是我的代码:

import time
import re
words = open('words.txt')
sentence = words.read()
uniquewords = []
positions = []
punctuation = re.findall(r"[\w']+|[.,!?;]", sentence)
for word in punctuation:
    if word not in uniquewords:
        uniquewords.append(word)
print("This file contains the words and punctuation ", uniquewords)
positions = [uniquewords.index(word) for word in punctuation]
recreated = " ".join([uniquewords[i] for i in positions])
print("In a list the text file words.txt can be shown as:")
print(positions)
print("Recreating sentence...")
print(recreated)

The program above does what it needs to, except it produces the following output: 上面的程序除了产生以下输出外,还执行所需的操作:

This file contains the words and punctuation ['Ask', 'not', 'what', 'your', 'country', 'can', 'do', 'for', 'you', ',', '!'] 该文件包含单词和标点符号['Ask','not','what','your','country','can','do','for','you',',','! ']

In a list the text file words.txt can be shown as: 在列表中,文本文件words.txt可以显示为:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 8, 5, 6, 7, 3, 4, 10] [0,1,2,3,4,5,6,7,8,9,0,2,8,5,6,7,3,4,10]

Recreating sentence... 重新建立句子...

Ask not what your country can do for you , Ask what you can do for your country ! 不要问你的国家可以为你做些什么,不要问你可以为你的国家做些什么!

The positions list starts at 0, so as normal I tried just doing this: 位置列表从0开始,因此像往常一样,我尝试这样做:

positions = [uniquewords.index(word)+1 for word in punctuation]

However this produces the error 但是,这会产生错误

  File "C:\Users\Sam\Desktop\COMPUTING TEMP FOLDER\task 3.py", line 13, in <module>
    recreated = " ".join([uniquewords[i] for i in positions])
  File "C:\Users\Sam\Desktop\COMPUTING TEMP FOLDER\task 3.py", line 13, in <listcomp>
    recreated = " ".join([uniquewords[i] for i in positions])
IndexError: list index out of range

How can I make the list start at 1 without getting this error? 如何使列表从1开始而不出现此错误? Any help would be greatly appreciated. 任何帮助将不胜感激。

Another small problem is that while the original string is 另一个小问题是,虽然原始字符串是

"Ask not what your country can do for you, Ask what you can do for your country!" “不要问您的国家可以为您做些什么,请问您可以为您的国家做些什么!”

the actual output is instead 实际的输出是

Ask not what your country can do for you , Ask what you can do for your country ! 不要问你的国家可以为你做些什么,不要问你可以为你的国家做些什么!

The problem is that you are incrementing every element of positions so that it displays as 1-indexed, then using that array when python is expecting 0-indexed. 问题是您要递增positions每个元素,以使其显示为1索引,然后在python期望为0索引时使用该数组。 Try using: 尝试使用:

recreated = " ".join([uniquewords[i-1] for i in positions])

instead 代替

Please check the below code. 请检查以下代码。 I changed bit for recreating string to solve space issue along with the indexing problem you were facing. 我更改了重新创建字符串的位,以解决空间问题以及您面临的索引问题。

import time
import re
words = open("val.txt",'r')
sentence = words.readline()
uniquewords = []
positions = []
punctuation = re.findall(r"[\w']+|[.,!?;]", sentence)
for word in punctuation:
    if word not in uniquewords:
        uniquewords.append(word)
print("This file contains the words and punctuation ", uniquewords)
positions = [uniquewords.index(word)+1 for word in punctuation]
#recreated = " ".join([uniquewords[i-1] for i in positions])
recreated = ''
for i in positions:
     w = uniquewords[i-1]
     if w not in '.,!?;':
          w = ' ' + w
     recreated = (recreated + w).strip()

print("In a list the text file words.txt can be shown as:")
print(positions)
print("Recreating sentence...")
print(recreated)

Output: 输出:

    C:\Users\dinesh_pundkar\Desktop>python c.py
('This file contains the words and punctuation ', ['Ask', 'not', 'what', 'your',
 'country', 'can', 'do', 'for', 'you', ',', '!'])
In a list the text file words.txt can be shown as:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 3, 9, 6, 7, 8, 4, 5, 11]
Recreating sentence...
Ask not what your country can do for you, Ask what you can do for your country!

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

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