Write index from a list in another list

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 list indexes that have value 0 in listAction?

    
asked by anonymous 19.10.2017 / 11:53

1 answer

4

Taking advantage of what you can do 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 indices in T whose value is zero

You can do it in a way 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

    
19.10.2017 / 11:56