当前位置: 代码迷 >> python >> 简单列表错误Python
  详细解决方案

简单列表错误Python

热度:68   发布时间:2023-07-14 09:48:39.0

在下面的代码中,我创建了一个列表列表,例如一个数组。 我开始收到“列表分配超出范围”错误。 作为此错误的解决方法,您在代码中可以看到2个额外的diceSumTable实例。 它会立即打印,但前面带有“ 2,3”。 在我的学习中,我不记得任何原因,如果已经定义了diceSumTable的每个实例,就会发生这种情况。

编辑:这是原始代码,没有应用解决方法。

def dice():

    diceSumTable = [2,3,4,5,6,7,8,9,10,11,12]

    diceSumTable[2] = [(1,1)]
    diceSumTable[3] = [(1,2),(2,1)]
    diceSumTable[4] = [(1,3),(2,2),(3,1)]
    diceSumTable[5] = [(1,4),(2,3),(3,2),(4,1)]
    diceSumTable[6] = [(1,5),(2,4),(3,3),(4,2),(5,1)]
    diceSumTable[7] = [(1,6),(2,5),(3,4),(4,3),(5,2),(6,1)]
    diceSumTable[8] = [(2,6),(3,5),(4,4),(5,3),(6,2)]
    diceSumTable[9] = [(3,6),(4,5),(5,4),(6,3)]
    diceSumTable[10] = [(4,6),(5,5),(6,4)]
    diceSumTable[11] = [(5,6),(6,5)]
    diceSumTable[12] = [(6,6)]

    #for illustrative purposes
    for i in diceSumTable:
        print i

dice()

如前所述,您将从索引#2开始开始索引diceSumTable,使条目0和1保持不变。 您得到的错误是因为您索引超出了数组的末尾。

对于您的问题,“ dict”可能是更好的解决方案:

diceSumTable = {}
diceSumTable[ 2 ] = [(1,1)]
diceSumTable[ 3 ] = [(1,2), (2,1)]

尝试这个:

def dice():

    diceSumTable = []  # define diceSumTable as a list
    diceSumTable.append((1,1))  # append the tuple to diceSumTable
    diceSumTable.append(((1,2),(2,1)))  # append a tuple of tuples to diceSumTable
    diceSumTable.append(((1,3),(2,2),(3,1)))
    diceSumTable.append(((1,4),(2,3),(3,2),(4,1)))
    diceSumTable.append(((1,5),(2,4),(3,3),(4,2),(5,1)))
    diceSumTable.append(((1,6),(2,5),(3,4),(4,3),(5,2),(6,1)))
    diceSumTable.append(((2,6),(3,5),(4,4),(5,3),(6,2)))
    diceSumTable.append(((3,6),(4,5),(5,4),(6,3)))
    diceSumTable.append(((4,6),(5,5),(6,4)))
    diceSumTable.append(((5,6),(6,5)))
    diceSumTable.append(((6,6)))

    #for illustrative purposes
    for i in diceSumTable:
        print i

dice()

您得到2、3的原因是,这是您在列表的第一位输入的内容,其他数字则替换为diceSumTable[x] =

设置diceSumTable[2] ,将替换列表中的第三个值(列表的索引为零-第一个值为name [0]),而不是当前保留的值2。

因此,在第一次调用后,您的diceSumTable等于[2,3,[(1,1)],5,6,...]

我认为您可以做的是,如其他地方提到的那样,对每个骰子组合使用diceSumTable = []然后使用diceSumTable.append((1,1))

您也可以使用字典。

diceSumTable = {}
diceSumTable[2] = [(1,1)]
diceSumTable[3] = [(1,2),(2,1)]
#etc

然后,您可以通过值而不是排名来访问。

>>>diceSumValue[3]
[(1,2),(2,1)]
>>>

您应该通过列表的索引而不是值来对列表进行索引

In [123]: lst = [3, 4, 5]

In [124]: lst[0] == 3 #item "3" is at index 0
Out[124]: True

In [125]: lst[3] #out of range
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-125-48d1ca706e8c> in <module>()
----> 1 lst[3] #out of range

IndexError: list index out of range

In [126]: lst[1] = 10 #if I want to change "4" in the list to "10"

In [127]: lst
Out[127]: [3, 10, 5]

您正在从2索引输入数据(表示数组中的第三个元素)。

diceSumTable = [2,3,4,5,6,7,8,9,10,11,12,13,14]

当你做

# Replacing third element
diceSumTable[2] = [(1,1)]

diceSumTable将像

diceSumTable = [2,3,[(1,1)],5,6,7,8,9,10,11,12,13,14]

Slly它将替换所有值。

您正在将列表中的条目的值与列表的索引混淆。

diceSumTable[3]对应于diceSumTable第四个条目(因为它们从0开始编号)。

您的第一行将创建一个列表diceSumTable其中包含13个条目,编号为0 ... 12。

您的下一行填充3d到13th条目(并丢弃之前的内容!)。

做您想做的事,您有几种选择。

1 /您可以执行自己的操作,但是忽略前两个条目。 这不是很pythonic ...

2 /您可以创建一个长度为11的列表,其中包含实际条目。 在这种情况下,最有效的方法是

 diceSumTable = [] ### empty list
 diceSumTable.append([(1,1)])
 diceSumTable.append([(1,2),(2,1)])
 #### etc.

3 /您可以使用dict 这可能最接近您想要的,尽管它在空间和时间上效率低下,因为您只想要连续的整数键(但是 ):

diceSumTable = {} ### empty dict
diceSumTable[2] = [(1,1)]
diceSumTable[3] = [(1,2),(2,1)]
#### etc.

(降价问题:是否有任何方法可以在项目符号列表或枚举列表中散布代码?)

除了这段代码,我不知道您会得到什么结果,我理解的是您想编码:

[(1, 1)]
[(1, 2), (2, 1)]
[(1, 3), (2, 2), (3, 1)]
[(1, 4), (2, 3), (3, 2), (4, 1)]
[(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]
[(1, 6), (2, 5), (3, 4), (4, 3), (5, 2), (6, 1)]
[(2, 6), (3, 5), (4, 4), (5, 3), (6, 2)]
[(3, 6), (4, 5), (5, 4), (6, 3)]
[(4, 6), (5, 5), (6, 4)]
[(5, 6), (6, 5)]
[(6, 6)]

在这种情况下,我认为您所犯的错误是相信以下行:

diceSumTable = [2,3,4,5,6,7,8,9,10,11,12,13,14]

给您一个索引从2开始到14结束的列表,这是错误的,因为在Python中,每个列表索引都从0开始,并且您无法更改它。 您给我的那一行实际上创建了一个列表,其中第一个索引为0,最后一个索引为12(列表的大小-1)。 并且您列出的是diceSumTable [0]是2 diceSumTable [1]是3,依此类推。

这会导致您有两个选择,要么接受从索引0开始的列表,要么想要继续使用想要的映射(我想是有原因的,您肯定希望将2与(1,1)关联, 3与(1,2),(2,1))仅使用diceSumTable [theNumberOfYourMapping -2]。 或如haavee所说,您可以使用dict。 但是在这种情况下,当您遍历字典时,您将不会拥有写入顺序的价值。 如果您想要的索引之间没有空隙,我将使用地图。 我认为,如果您可以解释一下使用更多您想做什么,那为什么会很棒,为什么要让2而不是0与[(1,1)]相关联。 除了打印外,您还想用其他方法做些什么吗? 总结一下,如果我理解您要执行的操作,我会编写代码:

def dice():
    diceSumTable = [[(1,1)],
                    [(1,2),(2,1)],
                    [(1,3),(2,2),(3,1)],
                    [(1,4),(2,3),(3,2),(4,1)],
                    [(1,5),(2,4),(3,3),(4,2),(5,1)],
                    [(1,6),(2,5),(3,4),(4,3),(5,2),(6,1)],
                    [(2,6),(3,5),(4,4),(5,3),(6,2)],
                    [(3,6),(4,5),(5,4),(6,3)],
                    [(4,6),(5,5),(6,4)],
                    [(5,6),(6,5)],
                    [(6,6)]]

    #this is like for value in diceSumTable but i will iterate to 0,1..10 in more
    for (i,value) in enumerate(diceSumTable):
        print str(i+2) + " is associated to " + str(value)

dice()

此外,如果您想进一步了解Python列表,我可以阅读:

对于Python字典: