2013-04-23 113 views
4

我有一个名为鹰这样与列表元素匹配的元组元素

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

元组列表和一个名为结果这样的名单:

['"', '"', '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. 

我甚至无法提取从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] 

同样的错误

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

+0

纵观所有的答案和响应,我觉得你的数据的坏;我的猜测是[2]访问多个答案。 – GoingTharn 2013-04-23 23:54:00

回答

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] 
+0

第一个步伐你4回应!但我跑了乌拉圭回合的代码,并口口声声说:在 R2 = – JPP 2013-04-23 21:10:04

+0

[项目的项目在电子商务如果项目[2] eagles_first_parts] IndexError:字符串索引超出范围 – JPP 2013-04-23 21:11:31

+0

我有蟒蛇2.7.3 32位赢得 – JPP 2013-04-23 21:12:28

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

我很欣赏你的意见的概率解决了感谢为y所有最后我用一本字典,但不能将其追加到列表 – JPP 2013-04-25 01:54:59

0

注:不写最高效的代码,但事情从你尝试下文。 我假设结果是表像列表

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

Nipun您好我已经试过你的代码,但它口口声声说: – JPP 2013-04-23 21:05:11

+0

回溯(最近最后一次通话): 文件 “C:\用户\ JP \桌面\ TT \ PLN \ programa_vsm \ prueba_ext_eagles.py”,第21行在 如果项目[2] ==鹰: IndexError:列表索引超出范围 – JPP 2013-04-23 21:05:27

+0

您的数据可能是坏的。尝试尝试/除了项目[2] ==老鹰线附近,并打印出违规的项目,如果异常的命中;它将帮助您确定您是否有数据问题或代码问题。 – GoingTharn 2013-04-23 23:57:23