我试图通过将字典键和一个numpy数组拼接成一个列表来制作一个新的元组。但是,我得到这个错误

TypeError: list indices must be integers, not tuple


这是我的代码

import numpy
import random


print(ly)
{(1, 3): 2, (5, 2): 1, (10, 1): 0}

def myFun(layout):
    possibilities = numpy.zeros(shape=(4,2))
    possibilities[0] = [1, 0]
    possibilities[1] = [-1, 0]
    possibilities[2] = [0, 1]
    possibilities[3] = [0, -1]

    newLayout = tuple()
    for i in layout:
        randomDirection = random.choice( possibilities )
        newLayout = newLayout + layout.keys()[i] + list(randomDirection)


口译员显示这条线

newLayout = newLayout + layout.keys()[i] + list(randomDirection)


因为有问题,但我不明白为什么

最佳答案

而不是您的代码newLayout = newLayout + layout.keys()[i] + list(randomDirection)

使用以下内容,
newLayout = newLayout + layout[i] + list(randomDirection)

会解决你的问题

08-20 02:43