在Python中搜索列表中字符串列表的精确匹配

2024-06-25 22:59:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个列表:

result = [['GELATIN', '76.0 mg', '40 %', 'Gelatin to 100.000 g Table 7 Capsule Quantity per unit flavouring dose Quantity per unit dose Components Nominal mass of capsule 76.0 mg In the cap (40 %) 30.4 mg flavouring agent corresponds to 1 '], 
          ['GELATIN', '45.6 mg', '14.5 %', 'Gelatin including water of a certain percentage'], 
          ['INK', '76.0 mg', '40 %', 'ink is used as diluent far as this is necessary for the markets. Table 4 Atenolol granules Components mg/capsule Granules Active ingredients Atenolol 50.00]]

以及字符串列表:

agent = ['Flavouring Agent', 'Anti-Tacking Agent', 'Preservative', 'Colouring Agent', 'Ph Adjusting Agent', 'Plasticizer', 'Diluent']

对于result中的每个子列表,我想从agent列表中搜索子列表中任意位置的元素。如果存在这样的元素,则将其作为新元素添加到子列表的开头。你知道吗

预期产量:

new_result = [['Flavouring Agent', 'GELATIN', '76.0 mg', '40 %', 'Gelatin to 100.000 g Table 7 Capsule Quantity per unit flavouring dose Quantity per unit dose Components Nominal mass of capsule 76.0 mg In the cap (40 %) 30.4 mg flavouring agent corresponds to 1 '], 
              ['GELATIN', '45.6 mg', '14.5 %', 'Gelatin including water of a certain percentage'], 
              ['Diluent', 'INK', '76.0 mg', '40 %', 'ink is used as diluent far as this is necessary for the markets. Table 4 Atenolol granules Components mg/capsule Granules Active ingredients Atenolol 50.00]]

这是因为'Flavouring Agent'出现在第一个子列表的最后一个元素中;'Diluent'出现在最后一个子列表的最后一个元素中。你知道吗

迄今为止的努力:

newl=[]                
for jj in agent:        
    for e in result:
        for ll in e:

            if jj in ll:
                #print(jj,ll)
                newl.append([jj,ll])
                break

Tags: to列表fortableunitresultquantityagent
2条回答

我认为你的问题是对网络等级的混淆,以及循环的顺序。假设您希望保留原始列表的顺序(而不是忽略元素),那么外部循环应该在列表上。然后您要检查列表中存在的agent中的任何单词。我们可以使用“flag”变量只添加一个“agent”:

res = []
for sub in result:
    new_sub = sub
    agent_found = False
    for ag in agent:
        if agent_found:
            break
        for item in sub:
            if ag.lower() in item.lower():
                new_sub = [ag] + new_sub
                agent_found = True
                break
    if not agent_found:
        new_sub = [" "] + new_sub
    res.append(new_sub)

提供:

[['Flavouring Agent', 'GELATIN', '76.0 mg', '40 %', 'Gelatin to 100.000 g Table 7 Capsule Quantity per unit flavouring dose Quantity per unit dose Components Nominal mass of capsule 76.0 mg In the cap (40 %) 30.4 mg flavouring agent corresponds to 1 '], 
 ['GELATIN', '45.6 mg', '14.5 %', 'Gelatin including water of a certain percentage'], 
 ['Diluent', 'INK', '76.0 mg', '40 %', 'ink is used as diluent far as this is necessary for the markets. Table 4 Atenolol granules Components mg/capsule Granules Active ingredients Atenolol 50.00']]
new_result=[]

for l in result:
    temp_results=[]
    for ag in agent:
        if ag in l:
            temp_results.append(ag)
    new_result.append(temp_result)

相关问题 更多 >