【问题标题】:list.append(obj) causes traceback - why is this?list.append(obj) 导致回溯 - 这是为什么?
【发布时间】:2012-08-05 21:58:17
【问题描述】:

考虑以下代码:

for url in urls:
        obj           = HtmlInfo()

        obj.url       = url 
        obj.html      = hc.get_html(url)
        obj.tag_count = hc.get_num_tags(obj.html, 0, True)
        htmlinfos.append(obj)

urls 是一个 url 列表,htmlinfos 在循环之前被初始化为一个空列表,当然:

htmlinfos = [ ]

然而,无论出于何种原因,当我尝试运行此代码时,我得到了 list-assignment index out of range 异常。

这可能是什么问题?注意,我的 Python 版本是 2.7,我使用的是 Django 的最新稳定版本(我相信是 1.4)

更新 - 追溯

Environment:


Request Method: GET
Request URL: http://xx.xxx.xxx.xx/xxx/0/test/

Django Version: 1.4.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'scrapper',
 'django_pdb')
Installed Middleware:
('django.middleware.cache.UpdateCacheMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django_pdb.middleware.PdbMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/usr/share/nginx/www/xxx/private/xxx/views.py" in test
  44.         return HttpResponse("Dis be er bad query yo " + test_id )
File "/usr/share/nginx/www/xxx/private/xxx/views.py" in __get_html_list
  23.     return list

Exception Type: IndexError at /xxx/0/test/
Exception Value: list assignment index out of range

更新 - __get_html_list()

def __get_html_list():
    hc = HtmlCounter()

    htmlinfos = [ ]

    #add more urls here for testing
    urls = [ '/usr/share/nginx/www/xxx/private/template/test/html_count_test.html' ]


    for url in urls:
        obj           = HtmlInfo()

        obj.url       = url 
        obj.html      = hc.get_html(url)
        obj.tag_count = hc.get_num_tags(obj.html)
        htmlinfos.append(obj)

    return htmlinfos

注意

最初htmlinfos 被简称为list,所以我在发布之前更改了它,重新运行它,我仍然收到同样的错误:/

更新 - get_html_tag_count()

为简洁起见,我想我也可以发布这个,以防这可能与问题有关:

def get_num_tags(self, html):

        if reset:
            self.reset()

        current_index = 0

        for char in html:

            if (char == "<"):

                close_index = html[current_index:].find("/>", current_index)

                if close_index == -1:
                    break
                else:
                    ++self._tag_count

            ++current_index

        return self._tag_count

【问题讨论】:

  • 请提供错误的完整追溯。有了当前的信息,我们所能做的就是猜测发生了什么。
  • 当您尝试执行 list_object[k] = value when k >= len(list_object) 之类的操作时,通常会引发此类异常
  • @aboutblank: 嗯,根据回溯,错误发生在return list 行——也许您的网络服务器需要重新启动才能使用当前版本的代码?
  • 似乎list 在某处被用作变量名——这是个坏主意,因为它隐藏了内置类的名称。
  • ++self._tag_count 不是 Python。它不会增加任何东西。

标签: python django list indexing indexoutofboundsexception


【解决方案1】:

尝试将++self._tag_count++current_index 分别替换为self._tag_count+=1current_index+=1

++var 适用于大多数语言,但 python 与大多数语言不同。

【讨论】:

    猜你喜欢
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 2011-01-14
    • 2021-07-24
    • 2013-02-25
    • 2014-01-01
    相关资源
    最近更新 更多