Python报错:list assignment index out of range

>>> a = []
>>> a[0] = 'dsfew'
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    a[0] = 'dsfew'
IndexError: list assignment index out of range

 

原因:列表a定义为一个空列表,a[0]这个元素不存在,所以为其赋值会报错

posted @ 2021-02-04 14:59  336699强强  阅读(640)  评论(0编辑  收藏  举报