Matching tuple elements with a list of elements

I have a list of tuples with eagle names such as

eagles= [("NCMS000","NCMS000"),("NCFP000","NCFP000"),("NCMS00D","NCMS00D"),("NCCS000","NCCS000"),("NCCP000","NCCP000"),("NCMN000","NCMN000"),("NCFN000","NCFN000"),("NP000G0","NP000G0"),("NP000G0","NP000G0"),... 

and a list called Result:

 ['"', '"', 'Fe', '1'] ['Hola', 'hola', 'I', '1'] ['como', 'como', 'CS', '0.999289'] ['estas', 'este', 'DD0FP0', '0.97043'] ['Bien', 'bien', 'NP00000', '1'] ['gracias', 'gracia', 'NCFP000', '1'] ['y', 'y', 'CC', '0.999962'] ['tu', 'tu', 'DP2CSS', '1'] ['yo', 'yo', 'PP1CSN00', '1'] ['estoy', 'estar', 'VAIP1S0', '1'] ['bien', 'bien', 'RG', '0.902728'] ['huevo', 'huevo', 'NCMS000', '0.916667'] ['calcio', 'calcio', 'NCMS000', '1'] ['leche', 'leche', 'NCFS000', '1'] ['proteina', 'proteina', 'NCFS000', '1'] ['Francisco', 'francisco', 'NP00000', '1'] ['1999', '1999', 'Z', '1'] ['"', '"', 'Fe', '1'] 

I need to create a function to compare the 3rd element of the result list with the first element of the eagles in a continuous loop. If they match, I need to return a list of lists with 4 items, for example:

  r = [['leche', 'leche', 'NCFS000', '1'],['proteina', 'proteina', 'NCFS000', '1'],['Francisco', 'francisco', 'NP00000', '1']] 

what i have done so far:

 def check(lst): return [x[2] for x in lst if (x[2] in y[0] for y in eagles)] IndexError: list index out of range. 

I can’t even extract the third item from the list and put it on an empty

 e = [x[0] for x in eagles] r = [item for item in e if item in Result] rg =[] for i in Result: rg = i[2] 

same mistake

What can I do? Any suggestion is appreciated.

+4
source share
3 answers

First, it is probably best to convert the eagles list to a dictionary ...

 >>> eagles = [("NCMS000","NCMS000"), ("NCFP000","NCFP000"), ...] >>> eagles_dict = dict(eagles) >>> print eagles_dict {'NCFP000': 'NCFP000', 'NCMS000': 'NCMS000', ...} 

... to make the search simpler and more efficient. Then you can use a simple list comprehension like ...

 >>> result = [['"', '"', 'Fe', '1'], ['Hola', 'hola', 'I', '1'], ...] >>> print [item for item in result if item[2] in eagles_dict] [['leche', 'leche', 'NCFS000', '1'], ...] 
+4
source

There may be a more efficient algorithm involving sorting, but if you just do it once or twice:

UPDATED to take into account the fact that your elements do not always have 4 elements.

 eagles_first_parts = [eagle[0] for eagle in eagles] r = [item for item in Result if len(item) > 2 and item[2] in eagles_first_parts] 
+1
source

NB: not writing the most efficient code, but something that follows from your attempts. I assume Result is a List of lists , for example:

 Result=[['"', '"', 'Fe', '1'],['Hola', 'hola', 'I', '1'], ['como', 'como', 'CS', '0.999289'], ['estas', 'este', 'DD0FP0', '0.97043'], ['Bien', 'bien', 'NP00000', '1'], ['gracias', 'gracia', 'NCFP000', '1'], ['y', 'y', 'CC', '0.999962'], ['tu', 'tu', 'DP2CSS', '1'], ['yo', 'yo', 'PP1CSN00', '1'], ['estoy', 'estar', 'VAIP1S0', '1'], ['bien', 'bien', 'RG', '0.902728'], ['huevo', 'huevo', 'NCMS000', '0.916667'], ['calcio', 'calcio', 'NCMS000', '1'], ['leche', 'leche', 'NCFS000', '1'], ['proteina', 'proteina', 'NCFS000', '1'], ['Francisco', 'francisco', 'NP00000', '1'], ['1999', '1999', 'Z', '1'], ['"', '"', 'Fe', '1']] 

Now, starting from where you left.

 e=[x[0] for x in eagles] 

Now initialize the empty list, r

 r=[] for item in Result: for eagle in e: if item[2]==eagle: r.append(item) print r 

which gives the result:

 [['gracias', 'gracia', 'NCFP000', '1'], ['huevo', 'huevo', 'NCMS000', '0.916667'], ['calcio', 'calcio', 'NCMS000', '1']] 
0
source

All Articles