Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
Identify index of all elements in a list comparing with another list

For instance I have a list A:

A = [100, 200, 300, 200, 400, 500, 600, 400, 700, 200, 500, 800]

And I have list B:

B = [100, 200, 200, 500, 600, 200, 500]

I need to identify the index of elements in B with comparison to A

I have tried:

list_index = [A.index(i) for i in B]

It returns:

[0, 1, 1, 5, 6, 1, 5]

But what I need is:

[0, 1, 3, 5, 6, 9, 10]

How can I solve it?

over 4 years ago · Santiago Trujillo
6 Respostas
Responde à pergunta

0

I loop through B and set the checked index to None in A. Thus the code will alter A.

A = [100, 200, 300, 200, 400, 500, 600, 400, 700, 200, 500, 800]
B = [100, 200, 200, 500, 600, 200, 500]

res = []
for i in B:
    res.append(A.index(i))
    A[A.index(i)] = None

print(res)

Output:

[0, 1, 3, 5, 6, 9, 10]
over 4 years ago · Santiago Trujillo Relatório

0

Here's what I would use:

A=[100,200,300,200,400,500,600,400,700,200,500,800]
B=[100,200,200,500,600,200,500]

list_index = []
removedElements = 0

for i in B:
  indexInA = A.index(i)
  A.pop(indexInA)
  list_index.append(indexInA+removedElements)
  removedElements+=1

print(list_index)
over 4 years ago · Santiago Trujillo Relatório

0

You can try something like this. Move over both lists and append the index when they are equal:

A = [100,200,300,200,400,500,600,400,700,200,500,800]

B = [100,200,200,500,600,200,500]

i, j = 0, 0
list_index = []
while j < len(B):
    if B[j] == A[i]:
        list_index.append(i)
        j += 1
    i += 1
print(list_index)

Output:

[0, 1, 3, 5, 6, 9, 10]

over 4 years ago · Santiago Trujillo Relatório

0

You can create a list called indices, and get the first index into it. Then iterate rest of the items in B, then take the slice of A from the last index in indices list and get the index of the item, add it to the last index + 1, then append it back to the indices list.

indices = [A.index(B[0])]
for i,v in enumerate(B[1:]):
    indices.append(A[indices[-1]+1:].index(v)+indices[-1]+1)


#indices: [0, 1, 3, 5, 6, 9, 10]
over 4 years ago · Santiago Trujillo Relatório

0

You can iterate through the enumeration of A to keep track of the indices and yield the values where they match:

A = [100,200,300,200,400,500,600,400,700,200,500,800]
B = [100,200,200,500,600,200,500]

def get_indices(A, B):
    a_it = enumerate(A)
    for n in B:
        for i, an in a_it:
            if n == an:
                yield i
                break
            
list(get_indices(A, B))
# [0, 1, 3, 5, 6, 9, 10]

This avoids using index() multiple times.

over 4 years ago · Santiago Trujillo Relatório

0

A = np.array([100,200,300,200,400,500,600,400,700,200,500,800])
B = [100, 200, 200, 500, 600, 200, 500]

idx = np.arange(len(A))
indices = {i: idx[A == i].tolist() for i in set(B)}
[indices[i].pop(0) for i in B]
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda