簡體   English   中英

將元組元素與列表元素相匹配

[英]matching tuple elements with list elements

我有一個名為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']

我需要創建一個函數來比較Result列表的第3項和一個連續循環中的eagles 1st項。 如果它們匹配,我需要返回帶有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]

同樣的錯誤

我能做什么? 任何建議表示贊賞。

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

可能有一個更有效的算法涉及排序,但如果你只是這樣做一次或兩次:

更新以考慮到您的商品並不總是有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]

注意:不是編寫最有效的代碼,而是從你的嘗試中得到的東西。 我假設,結果就像列表列表

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM