Filter a specific element within a stripe within another list

Asked

Viewed 96 times

0

I have a filter to find the index of a stripe inside another list (found in the author group Anderson Carlos)

fila = [['vitor','5'], ['vitor', '4']]

def sdb(filadb,nome,elemento):

if not (filadb):
    return None
else:
    filadb_l = filadb[0]
    p = [(filadb_l.index(x), x.index(nome)) for x in filadb_l if nome in x] # linha 0 posicao 1
    if p == []:
        return None
    else:
        p_e = [(fila_pe.index(x), x.index(elemento)) for x in fila_pe if elemento in x] # linha 0 posicao 1
        p_linha = filadb_l[p[0][0]]
        posicao_e = fila_pe[p_e[0][0]][1]       
        p_elemento = p_linha[posicao_e]
        result = [p_linha]
        return p_elemento

function return:

[5] That way I have only one element.

I’d like you to return the two elements to me. return:

['5','4']

  • Possibly duplicated. https://answall.com/questions/210238/comorbuscar-um-elemento-em-uma-lista-que-est%C3%A1-within-another-list

  • It’s not the same thing. As a website, it’s another question. I’ve seen this post.

  • What exactly was your attempt? Put the code in the question, please.

  • Anderson Carlos Woss 10 months ago a question was asked "How to find an element in a list that is inside another list". Vc made a simple way that helped me a lot "[(list.index(x), x.index(value)) for x in list if value in x]". Thanks in advance

  • I managed to solve it. Later put the formula.

  • What do you need to do? Find the index of a value or find all values in a given index?

Show 1 more comment

1 answer

1


filaa = [(('vitor','5'), ('vitor', '4'))]
def sadb(filadb,nome):
    l = []
    if not (filadb):
        return None
    else:
        filadb_l = filadb[0]
        p = [(filadb_l.index(x), x.index(nome)) for x in filadb_l if nome in x] # linha 0 posicao 1
        if p == []:
            return None
        else:
            for item in p:
                lista = filadb[0][item[0]]
                n = lista[1]
                if not 'valvula' in l:
                    l.append(n)
            return l

print sadb(filaa, 'vitor')

['5', '4']

The code is not optimized, but it works q eh a beauty.

Browser other questions tagged

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