异常值:列表分配索引超出范围

时间:2014-10-06 16:42:27

标签: python django python-2.7

以下Django视图不断抛出错误

  

异常值:未定义全局名称“图像”

views.py

PATH_ONE_IMAGES = ['P1D1.jpg', 'P2D2.jpg', 'P3D3.jpg', 'P4D4.jpg', 'P5D5.jpg', 'P6D6.jpg', 'P7D7.jpg', 'P8D8.jpg', 'P9D9.jpg']


class SurveyWizardOne(SessionWizardView):                      
    def get_context_data(self, form, **kwargs):
        context = super(SurveyWizardOne, self).get_context_data(form, **kwargs)  
        if self.steps.current in ['5','6','7','8','9','10','11','12','13','14','15','16']:
            print '\nThe available list of Path_One images is', PATH_ONE_IMAGES

            images = []
            step = int(self.steps.current)

            if step in (5, 6, 7):
                images[step - 5] = image = random.choice(PATH_ONE_IMAGES)          
                PATH_ONE_IMAGES.remove(image)
                context['display_image'] = image

            elif step == 8:
                context['first_image'] = images[0]
                context['second_image'] = images[1]
                context['third_image'] = images[2]             

            elif step in (9, 10, 11):
                images[3 + step - 9] = image = random.choice(PATH_ONE_IMAGES)          
                PATH_ONE_IMAGES.remove(image)
                context['display_image'] = image

            elif step == 12:
                context['fourth_image'] = images[3]
                context['fifth_image'] = images[4]
                context['sixth_image'] = images[5]

            elif step in (13, 14, 15):
                images[6 + step - 13] = image = random.choice(PATH_ONE_IMAGES)          
                PATH_ONE_IMAGES.remove(image)
                context['display_image'] = image

            else:# self.steps.current == '16':
                context['fourth_image'] = images[6]
                context['fifth_image'] = images[7]
                context['sixth_image'] = images[8] 

            steps = ['5','6','7','9','10','11','13','14','15']              

            context.update({'steps': steps})

        return context 

当我用

定义'images'时
        ....
        if self.steps.current in ['5','6','7','8','9','10','11','12','13','14','15','16']:

            images = []
            step = int(self.steps.current)

            if step in (5, 6, 7):
        ....

我得到了

  

异常值:列表分配索引超出范围

如果我为其添加值

images = [0,1,2,3,4]

它们变得硬编码,不会取PATH_ONE_IMAGES

的值

谁能看到我在这里做错了什么?如何定义图像数组以使其列表分配索引不在范围之外,因此可以更新?

2 个答案:

答案 0 :(得分:1)

您发布的代码可能缺少重要信息,但是,假设没有重要信息丢失,我相信罪魁祸首是您的代码(当删除无用的详细信息时)看起来像:

images = []
for x in something:
    images[index] = x

有效。您无法使用正常分配项添加到列表中:

In [1]: images = []
   ...: images[0] = 1
   ...: 
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-1-d32b85fdcde7> in <module>()
      1 images = []
----> 2 images[0] = 1
      3 

IndexError: list assignment index out of range

当您使用name[index] = value表示法时,您只能修改索引index处的值,但此类索引必须已存在于列表中,否则会引发IndexError

您必须使用append添加到列表末尾,或使用insert在特定索引中添加项目:

In [2]: images = []

In [3]: images.append(1)

In [4]: images
Out[4]: [1]

In [5]: images.insert(0, 2)

In [6]: images
Out[6]: [2, 1]

=表示法只有在使用切片时才能更改列表的大小:

In [7]: images[2:10] = range(8)

In [8]: images
Out[8]: [2, 1, 0, 1, 2, 3, 4, 5, 6, 7]

事实上,通常使用切片的操作不会引发IndexError。即使images是一个空列表,您也可以评估images[100:1000],它只会评估为一个空列表(因为索引超出范围会返回空列表),而images[100]会引发一个空列表IndexError)。

答案 1 :(得分:0)

使用append函数将元素添加到列表

例如

images=[]
images.append(5)
images.append(6)
print images
[5,6]

因为step5开始意味着images列表从零开始,这是使用append语句正确插入的

所以代码应该是

f step in (5, 6, 7):
                images.append(random.choice(PATH_ONE_IMAGES))
                PATH_ONE_IMAGES.remove(image)
                context['display_image'] = image