【问题标题】:Python: list index out of range - while/for loopPython:列表索引超出范围 - while/for 循环
【发布时间】:2015-03-07 14:10:53
【问题描述】:

我有一个清单

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

我想对句子进行情感分析。之后,我想将结果存储在如下所示的列表中:

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

为此,我尝试了以下代码:

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

len(abc) 约为 7500。我以 x 为 2 和 i 为 3 开始,因为我不想使用列表的前两个元素。

无论我尝试什么,我都会不断收到错误“列表索引超出范围”(while、for 循环...)

谁能帮帮我?谢谢!

【问题讨论】:

  • 请显示错误的完整堆栈跟踪。
  • x = x + 2; i = i + 2 你的循环中吗?
  • @yole 我正在尝试重现我所做的,但现在我根本没有得到任何输出。就好像 Python 正在经历一个“无限循环”。
  • @ForceBru 是的,我在原始帖子中编辑了它
  • 看起来您在“结果”函数的第 6 行错过了“)”字符...

标签: python list


【解决方案1】:

正如 cmets 所提到的 - 如果没有堆栈跟踪,我们将无法帮助您查找代码中的错误。但是这样解决你的问题很容易:

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])

您可能想了解built-in 使程序员生活更轻松的函数。 (如果 range 已经有了,为什么还要担心增加呢?)

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

【讨论】:

    【解决方案2】:

    试试这个

    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])
    

    只需要一个索引。重要的是知道何时停止。

    另外,看看extend和append的区别

    最后,我建议您将数据存储为字典列表而不是列表列表。这使您可以通过字段名称而不是索引来访问项目,从而使代码更简洁。

    【讨论】:

    • 能不能给我一个亲爱的并缩进附加。无法在移动设备上缩进...
    【解决方案3】:

    如果您一次需要列表中的两个元素,您可以使用生成器,然后将元素传递给您的分类器:

    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
    

    因此,要创建 xyz 列表列表,您可以使用列表推导:

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

    【讨论】:

      【解决方案4】:

      这很简单。你可以试试:

      >>> 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'], .....]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        • 2022-06-13
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多