4

我有一个像这样命名为 eagles 的元组列表

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

和一个名为 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']

我需要创建一个函数来在一种连续循环中将结果列表的第 3 项与老鹰第 1 项进行比较。如果它们匹配,我需要返回一个包含 4 个元素的列表列表,例如:

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

到目前为止我做了什么:

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.

我什至无法从列表中提取第三个元素并将其放在一个空元素上

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]

同样的错误

我能做些什么?任何建议表示赞赏。

4

3 回答 3

4

首先,将eagles列表转换为字典可能会更好......

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

...使查找更简单、更高效。然后你可以使用一个简单的列表理解,比如......

>>> result = [['"', '"', 'Fe', '1'], ['Hola', 'hola', 'I', '1'], ...]
>>> print [item for item in result if item[2] in eagles_dict]
[['leche', 'leche', 'NCFS000', '1'], ...]
于 2013-04-23T13:03:57.247 回答
1

可能有一种更有效的算法涉及排序,但如果你只是这样做一两次:

更新考虑到您的物品并不总是有 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]
于 2013-04-23T12:58:39.450 回答
0

注意:不是编写最有效的代码,而是从您的尝试中得出的。我假设 Result 是一个列表列表例如:

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

现在从你离开的地方开始。

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

现在,初始化一个空列表 r

r=[]

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

这给出了输出:

[['gracias', 'gracia', 'NCFP000', '1'],
['huevo', 'huevo', 'NCMS000', '0.916667'],
['calcio', 'calcio', 'NCMS000', '1']]
于 2013-04-23T13:09:13.320 回答