How to determine the index of items in one list in another

Asked

Viewed 485 times

8

Work with python 2.7. Whereas the following:

S_names=["A", "B", "C", "D", "E", "F", "G"]
S_values=[1,3,4,2,5,8,10]
other=["Z","W","B","S","A","E","X","T","D","G","L","C","F"]

I need to find in what position other the elements of S_names. To obtain the list of indices of the elements of S_names in other, resulting in the list Result:

Result=[4,2,11,8,5,12,9] 

I tried to work with dictionaries doing the following:

def indicesDeElementoNaLista_s(elementoProcurado, lista):
    return [i for (i, elemento) in lista if elemento == elementoProcurado]

def elementosNasPosicoes_s(lista, posicoes):
    return [lista[i] for i in posicoes]

carg={}
for elemento in S_names:
    posicoes=indicesDeElementoNaLista_s(elemento,other)
    elementosCorrespondentes=elementosNasPosicoes_s(S_values,posicoes)
    cargas_sub[elemento]=elementosCorrespondentes_s

But I’ve made a lot of mistakes and I don’t understand what’s wrong... How can I get around this situation?

  • What relationship are you looking to make with the S_values list?

4 answers

2


To get the Result you want, you can do it as follows:

>>> S_names = ["A", "B", "C", "D", "E", "F", "G"]
>>> S_values =[1, 3, 4, 2, 5, 8, 10]
>>> other = ["Z", "W", "B", "S", "A", "E", "X", "T", "D", "G", "L", "C", "F"]
>>> 
>>> Result = [other.index(i) for i in S_names if i in other]
>>> print(Result)
[4, 2, 11, 8, 5, 12, 9]

However, this relates only the S_names and other lists. Seeing the rest of your code, I imagine you want a dictionary with the name and position of the items that repeat in the list 'other' and the positions that repeat in the list S_values. If that’s it, you can do it like this:

>>> posicoes = [other.index(i) for i in S_names if i in other]
>>> print(posicoes)
[4, 2, 11, 8, 5, 12, 9]
>>>
>>> elementosCorrespondentes = [S_values[i] for i in range(len(S_values)) if S_values[i] == posicoes[i]]
>>> print(elementosCorrespondentes)
[5]
>>>
>>> carg = {S_names[i]:i for i in elementosCorrespondentes}
>>> print(carg)
{'F': 5}

1

Hello! See if this helps you:

S_names=["A", "B", "C", "D", "E", "F", "G"]
S_values=[1,3,4,2,5,8,10]
other=["Z","W","B","S","A","E","X","T","D","G","L","C","F"]

result = []
for name in S_names:
    result.append(other.index(name))

print(result)

1

You can do it like this:

for name in S_names:
    for x in other:
        if name == x:
            Result.append(other.index(x))

1

Here is a way in which the final variable will store all the present letters of S_names present in other and their index (key):

results = []
for i in other:
    if i in S_names:
        print i+ ' - ' +str(other.index(i))
        results.append({'letter': i, 'index': other.index(i)})
print results

# Resultados: [{'index': 2, 'letter': 'B'}, {'index': 4, 'letter': 'A'}, {'index': 5, 'letter': 'E'}, {'index': 8, 'letter': 'D'}, {'index': 9, 'letter': 'G'}, {'index': 11, 'letter': 'C'}, {'index': 12, 'letter': 'F'}]

Making with comprehensive list:

results = [{'letter':i, 'index':other.index(i)} for i in other if i in S_names]
print results

# Resultados: [{'index': 2, 'letter': 'B'}, {'index': 4, 'letter': 'A'}, {'index': 5, 'letter': 'E'}, {'index': 8, 'letter': 'D'}, {'index': 9, 'letter': 'G'}, {'index': 11, 'letter': 'C'}, {'index': 12, 'letter': 'F'}]

I don’t quite understand where the S_values come in, but I’ll play with what I think you want about S_values:

In case you want to see if the indices in other of the letters of S_names are in the list of S_values:

results = [{'letter':i, 'index':other.index(i)} for i in other if i in S_names and other.index(i) in S_values]
print results

# Resultados: [{'index': 2, 'letter': 'B'}, {'index': 4, 'letter': 'A'}, {'index': 5, 'letter': 'E'}, {'index': 8, 'letter': 'D'}]

cycle is equivalent to this:

results = []
for i in other:
    if i in S_names and other.index(i) in S_values:
        results.append({'letter': i, 'index': other.index(i)})
print results

# Resultados: [{'index': 2, 'letter': 'B'}, {'index': 4, 'letter': 'A'}, {'index': 5, 'letter': 'E'}, {'index': 8, 'letter': 'D'}]

Browser other questions tagged

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