user3067923
user3067923

Reputation: 457

making new tuple from list and dictionary

I am trying to make a new tuple by splicing together a dictionary key and a numpy array turned into a list. However, I get this error

TypeError: list indices must be integers, not tuple

Here is my code

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)

the interpreter shows this line

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

as being problematic but I do not understand why

Upvotes: 2

Views: 111

Answers (1)

Sanket
Sanket

Reputation: 745

instead of in your code newLayout = newLayout + layout.keys()[i] + list(randomDirection)

Use following, newLayout = newLayout + layout[i] + list(randomDirection)

will solve your problem

Upvotes: 3

Related Questions