2013-04-23 135 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