IndexError: list assignment index out of range - Python with an array

前端 未结 1 2015
终归单人心
终归单人心 2021-01-16 01:47

I recently started to use python and i am still newbie with many things of the language. This piece of code should print a serie rows (Ex.:[47.815, 47.54, 48.065, 57.45]) th

相关标签:
1条回答
  • It seems like your problem is pretty straightforward. You probably have a list named row and you're trying to assign values to non-existent indices

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

    If however you appended a new list into the row list like I think you want to do

    >>> row = []
    >>> row.append([1, 2, 3, 4])
    >>> row
    [[1, 2, 3, 4]]
    >>> row[0][0]
    1
    

    This would work fine. If you're trying to triple nest things

    >>> row = []
    >>> row.append([[1,2,3,4]])
    >>> row
    [[[1, 2, 3, 4]]]
    >>> row[0].append([-1, -2, -3, -4])
    >>> row
    [[[1, 2, 3, 4], [-1, -2, -3, -4]]]
    

    You can use the same syntax

    0 讨论(0)
提交回复
热议问题