0

I need to subtract the last element and buid a vector like [1,1,1,...]. I have this function:

def vectores(lista):
    r=[]
    for e in lista:
        r.append(e[2])
        return r

where

lista = [['pintor', 'NCMS000', 1], ['ser', 'VSIS3S0', 1], ['muralista', 'AQ0CS0', 1], ['diego_rivera', 'NP00000', 1], ['frida_kahlo', 'NP00000', 1], ['caso', 'NCMS000', 1]]

But the function is returning [1]; what can I do?

4

1 回答 1

8

您将返回循环的第一次迭代。将 return 语句移到-loop之外:for

def vectores(lista):
    r=[]
    for e in lista:
        r.append(e[2])
    return r  # here

或者只使用列表理解:

def vectores(lista):
    return [e[2] for e in lista]
于 2013-08-19T22:21:52.303 回答