Python-理解错误:IndexError:列表索引超出范围

发布于 2024-10-29 04:38:35 字数 2253 浏览 4 评论 0原文

我对 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:抱歉我的英语不好。

此致,

I'm fairly new to python. I have an error that I need to understand.

The code:

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

And the error that appears sometimes because of the nature of the random:

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

I'am not getting to the error, the list "feeds_updates" is a list with 8 elements, I think is well declareted and the RANDOM will choose one out of the 8...

Can someone give me a clue on what is happenning here?

PS: Sorry for my bad english.

Best Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑梦风尘 2024-11-05 04:38:35

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

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

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

Using range for iteration is nearly always not the best way. In Python you can iterate directly over a list, dict, set etc.:

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

Obviously d.entries[i] triggers the error because that list contains less than 8 items (feeds_updates may contain 8, but you are not iterating over that list).

街角卖回忆 2024-11-05 04:38:35

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

d.entries has less than 8 elements. Iterate over d.entries directly instead of some disconnected range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文