【问题标题】:Python - Understanding error: IndexError: list index out of rangePython - 理解错误:IndexError:列表索引超出范围
【发布时间】:2011-04-01 09:50:15
【问题描述】:

我对 python 还很陌生。我有一个错误需要理解。

代码:

config.py:

# Vou definir os feeds
feeds_updates = [{"feedurl": "http://aaa1.com/rss/punch.rss", "linktoourpage": "http://www.ha.com/fun.htm"},
                 {"feedurl": "http://aaa2.com/rss.xml", "linktoourpage": "http://www.ha.com/fun.htm"},
                 {"feedurl": "http://aaa3.com/Heaven", "linktoourpage": "http://www.ha.com/fun.htm"},
                 {"feedurl": "http://aaa4.com/feed.php", "linktoourpage": "http://www.ha.com/fun.htm"},
                 {"feedurl": "http://aaa5.com/index.php?format=feed&type=rss", "linktoourpage": "http://www.ha.com/fun.htm"},
                 {"feedurl": "http://aaa6.com/rss.xml", "linktoourpage": "http://www.ha.com/fun.htm"},
                 {"feedurl": "http://aaa7.com/?format=xml", "linktoourpage": "http://www.ha.com/fun.htm"},
                 {"feedurl": "http://aaa8/site/component/rsssyndicator/?feed_id=1", "linktoourpage": "http://www.ha.com/fun.htm"}]

twitterC.py

# -*- coding: utf-8 -*-
import config   # Ficheiro de configuracao
import twitter
import random
import sqlite3
import time
import bitly_api #https://github.com/bitly/bitly-api-python
import feedparser

...

# Vou escolher um feed ao acaso
feed_a_enviar = random.choice(config.feeds_updates)
# Vou apanhar o conteudo do feed
d = feedparser.parse(feed_a_enviar["feedurl"])
# Vou definir quantos feeds quero ter no i
i = range(8)
print i
# Vou meter para "updates" 10 entradas do feed
updates = []
for i in range(8):
    updates.append([{"url": feed_a_enviar["linktoourpage"], "msg": d.entries[i].title + ", "}])
# Vou escolher ums entrada ao acaso
print updates # p debug so
update_to_send = random.choice(updates)

print update_to_send # Para efeitos de debug

以及由于随机的性质有时会出现的错误:

Traceback (most recent call last):
  File "C:\Users\anlopes\workspace\redes_sociais\src\twitterC.py", line 77, in <module>
    updates.append([{"url": feed_a_enviar["linktoourpage"], "msg": d.entries[i].title + ", "}])
IndexError: list index out of range

我没有遇到错误,列表“feeds_updates”是一个包含 8 个元素的列表,我认为声明得很好,随机数将从 8 个中选择一个...

谁能告诉我这里发生了什么?

PS:对不起,我的英语不好。

最好的问候,

【问题讨论】:

  • 您知道索引从零开始吗? 8 个元素的索引为 0 到 7?
  • @S. Lott:range(8)[0,1,2,3,4,5,6,7],所以这不是问题所在。
  • @Tim Pietzcker:当然,我们都假设提供的代码实际上在feed_updates 列表中有8 个项目,不是吗?是的,证据很好,但肯定有一些事情没有在提供的代码 sn-p 中显示。
  • @S. Lott:feed_updates 中的项目数量无关紧要,因为他正在迭代一个完全不同的列表。看我的回答。

标签: python list outofrangeexception


【解决方案1】:

使用range 进行迭代几乎总是不是最好的方法。在 Python 中,您可以直接遍历列表、字典、集合等:

for item in d.entries:
    updates.append([{"url": feed_a_enviar["linktoourpage"], "msg": item.title + ", "}])

显然d.entries[i] 会触发错误,因为该列表包含少于 8 个项目(feeds_updates 可能包含 8 个,但您没有遍历该列表)。

【讨论】:

    【解决方案2】:

    d.entries 的元素少于 8 个。直接迭代d.entries,而不是一些不连贯的范围。

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 2015-01-28
      相关资源
      最近更新 更多