Write Dice from one list to another list

Asked

Viewed 48 times

2

T = [0,0,0,1,0,0,0,-1,0]
def acoes(T):
    listaAction=[]
    for i in T:
            if(T[i]==0):
                    listaAction.append(T.index(i))

    return listaAction

print(acoes(T))

How do I write T-list indexes that have value 0 in listAction?

1 answer

4


Taking advantage of what you have you can do like this:

def acoes(T):
    listaAction=[]
    for idx, i in enumerate(T):
        if(i==0):
            listaAction.append(idx)
    return listaAction

T = [0,0,0,1,0,0,0,-1,0]
print(acoes(T)) # [0, 1, 2, 4, 5, 6, 8]

acoes(T) will return a list with the indices in T zero

You can do it even more pythonica:

def acoes(T):
    return [idx for idx, i in enumerate(T) if i == 0]

T = [0,0,0,1,0,0,0,-1,0]
print(acoes(T)) # [0, 1, 2, 4, 5, 6, 8]

DEMONSTRATION of both examples

Browser other questions tagged

You are not signed in. Login or sign up in order to post.