How can I match tuple elements with list elements

0 votes

I have a list of tuples named eagles like this

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

and a list named Result like this:

['"', '"', '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 item of Result list with the eagles 1st item in a kind of continuous loop. If they match, I need to return a list of lists w/ the 4 elements, e.g:

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

what I did 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 3rd element from the list and put it on an empty one

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 error

What can I do? Any suggestion is appreciated.

Sep 7, 2018 in Python by charlie_brown
• 7,720 points
2,704 views

1 answer to this question.

0 votes

It's better if you convert your eagles tuple into a dictionary and  then handle it because you seem to be dealing with key value pairs

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

...to make the lookups 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'], ...]
answered Sep 7, 2018 by aryya
• 7,460 points

Related Questions In Python

+2 votes
4 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,510 points
122,466 views
+2 votes
2 answers

In a list of dictionaries, how can I find the minimum calue in a common dictionary field.

There are several options. Here is a ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
1,432 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,479 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
0 votes
1 answer

How can I expose callbacks to Fortran using Python

The code that I've written below. The ...READ MORE

answered Aug 24, 2018 in Python by aryya
• 7,460 points
1,639 views
0 votes
2 answers

How can I install external binaries using portable python

0110100001000101001010101001011010100100111100101001 READ MORE

answered Jan 24, 2019 in Python by anonymous
2,239 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP