简体   繁体   English

Python:列表索引超出范围-while / for循环

[英]Python: list index out of range - while/for loop

I have a list 我有一个清单

abc = ['date1','sentence1','date2','sentence2'...]

I want to do sentiment analysis on the sentences. 我想对句子进行情感分析。 After that I want to store the results in a list that looks like: 之后,我想将结果存储在一个类似以下的列表中:

xyz =[['date1','sentence1','sentiment1'],['date2','sentence2','sentiment2']...]

For this I have tried following code: 为此,我尝试了以下代码:

def result(doc):
    x = 2
    i = 3
    for lijn in doc:
        sentiment = classifier.classify(word_feats_test(doc[i]))
        xyz.extend(([doc[x],doc[i],sentiment])
        x = x + 2
        i = i + 2

The len(abc) is about 7500. I start out with x as 2 and i as 3, as I don't want to use the first two elements of the list. len(abc)约为7500。由于我不想使用列表的前两个元素,因此我以x为2和​​i为3开始。

I keep on getting the error 'list index out of range', no matter what I try (while, for loops...) 无论我尝试什么,我都会不断得到错误“列表索引超出范围”(同时,对于循环...)

Can anybody help me out? 有人可以帮我吗? Thank you! 谢谢!

As comments mentioned - we won't be able to help You with finding error in Your code without stacktrace. 如评论所述-没有栈跟踪,我们将无法帮助您发现代码中的错误。 But it is easy to solve Your problem like this: 但是很容易解决这样的问题:

xyz = []
def result(abc):
    for item in xrange(0, len(abc), 2): # replace xrange with range in python3
        #sentiment = classifier.classify(word_feats_test(abc[item]))
        sentiment = "sentiment" + str(1 + (item + 1) / 2) 
        xyz.append([abc[item], abc[item + 1], sentiment])

You might want to read about built-in functions that makes programmers life easy. 您可能想了解使程序员轻松工作的内置函数。 (Why worry about incrementing if range has that already?) (为什么要担心范围已经增加呢?)

#output
[['date1', 'sentence1', 'sentiment1'],
 ['date2', 'sentence2', 'sentiment2'],
 ['date3', 'sentence3', 'sentiment3'],
 ['date4', 'sentence4', 'sentiment4'],
 ['date5', 'sentence5', 'sentiment5']]

Try this 尝试这个

i =0
for i in xrange(0,len(doc) -1)
    date = doc[i]
    sentence = doc[i + 1]
    sentiment = classifier.classify(word_feats_test(sentence))
    xyz.append([date,sentence,classifier])

Only need one index. 只需要一个索引。 The important thing is knowing when to stop. 重要的是知道何时停止。

Also, check out the difference between extend and append 另外,检查一下extend和append之间的区别

Finally I would suggest you store your data as a list of dictionaries rather than a list of lists. 最后,我建议您将数据存储为词典列表而不是列表列表。 That lets you access the items by field name rather than index , which makes for cleaner code. 这样,您就可以通过字段名而不是index来访问项目,这使代码更简洁。

If you want two elements from your list at a time, you can use a generator then pass the element/s to your classifier: 如果一次要从列表中选择两个元素,则可以使用生成器,然后将元素传递给分类器:

abc = ["ignore","ignore",'date1','sentence1','date2','sentence2']

from itertools import islice


def iter_doc(doc, skip=False):
    it = iter(doc)
    if skip: # if  skip is set, start from index doc[skip:]
         it = iter(islice(it, skip, None))
    date, sent = next(it), next(it)
    while date and sent:
        yield date, sent
        date, sent = next(it, ""), next(it, "")


for d, sen in result(abc, 2): # skip set to to so we ignore first two elements
    print(d, sen)

date1 sentence1
date2 sentence2

So to create you list of lists xyz you can use a list comprehension: 因此,要创建列表xyz列表,可以使用列表理解:

xyz = [ [d,sen,classifier.classify(word_feats_test(sen))] for d, sen in iter_doc(abc, 2)]

It's simple. 这很简单。 you can try it: 你可以试试:

>>> abc = ['date1','sentence1','date2','sentence2'...]    
>>> xyz = [[ abc[i], abc[i+1], "sentiment"+ str(i/2 + 1)] for i in range(0, len(abc), 2) ]
>>> xyz
output : [['date1', 'sentence1', 'sentiment1'], ['date2', 'sentence2', 'sentiment2'], .....]

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

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