How to determine the index of elements in a list in another

7

Work with python 2.7 . Considering 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 of other are the S_names elements. To get the list of indexes of S_names elements in other , resulting in the Result :

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

I tried working 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 got several errors and I do not understand what's wrong ... How can I get around this?

    
asked by anonymous 23.05.2016 / 20:23

4 answers

1

To get the Result you want, you can do 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"]
>>> 
>>> Result = [other.index(i) for i in S_names if i in other]
>>> print(Result)
[4, 2, 11, 8, 5, 12, 9]

However, this lists 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 repeating items in the 'other' list and the repeating positions in the S_values list. If so, you can do 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}
    
23.05.2016 / 22:15
1

Hello! see if it helps:

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)
    
23.05.2016 / 21:01
1

You can do this:

for name in S_names:
    for x in other:
        if name == x:
            Result.append(other.index(x))
    
24.05.2016 / 00:22
1

Here is a way whose final variable will store all the present letters of S_names present in other and their respective 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'}]

Doing with list comprehension:

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 did not really understand where the S_values come in, but I'll play with what I think you want about S_values:

If you want to see if the indices in other of the 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'}]
    
24.05.2016 / 10:31