简体   繁体   English

将元组元素与列表元素相匹配

[英]matching tuple elements with list elements

I have a list of tuples named eagles like this 我有一个名为eagles的元组列表

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: 和一个名为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 item of Result list with the eagles 1st item in a kind of continuous loop. 我需要创建一个函数来比较Result列表的第3项和一个连续循环中的eagles 1st项。 If they match, I need to return a list of lists w/ the 4 elements, eg: 如果它们匹配,我需要返回带有4个元素的列表列表,例如:

 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 我甚至无法从列表中提取第3个元素并将其放在空元素中

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. 任何建议表示赞赏。

First, of all, it's probably better to convert the eagles list into a dictionary... 首先,将eagles列表转换为字典可能更好......

>>> 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'], ...]

There is probably a more efficient algorithm involving sorting but if you're just doing this once or twice: 可能有一个更有效的算法涉及排序,但如果你只是这样做一次或两次:

UPDATED to take into account the fact that your items don't always have 4 elements. 更新以考虑到您的商品并不总是有4个元素的事实。

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]

NB: Not writing most efficient code, but something which follows from your attempts. 注意:不是编写最有效的代码,而是从你的尝试中得到的东西。 I am assuming that Result is a List of Lists like: 我假设,结果就像列表列表

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 an empty list, r 现在,初始化一个空列表,r

r=[]

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

which gives the output: 它给出了输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM