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

在Python中搜索列表中字符串列表的精确匹配,python,python-3.x,string,list,loops,Python,Python 3.x,String,List,Loops,我有一份清单: 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 '],

我有一份清单:

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]]
这是因为
“调味剂”
出现在第一个子列表的最后一个元素中;并且,
“稀释剂”
出现在最后一个子列表的最后一个元素中

迄今为止的努力:

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
new_result=[]
对于结果中的l:
临时结果=[]
对于ag in代理:
如果ag在l中:
临时结果追加(ag)
新结果追加(临时结果)

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

res=[]
对于结果中的子项:
新建_sub=sub
找到的代理=False
对于ag in代理:
如果发现了代理(u):
打破
对于子项中的项目:
如果.lower()项中的ag.lower():
新分区=[ag]+新分区
agent\u found=True
打破
如果未找到代理,请执行以下操作:
新建分区=[“”]+新建分区
res.append(新的_子项)
给出:

[['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'
,而
结果
列表中有
'Flavouring Agent'
这将返回空列表。你能解释一下你是如何在新结果的第一个列表中列出“调味剂”的吗“调味剂不在输入列表的第一个列表中?@user1558604它位于最后一个元素的字符串中。您的代码会搜索代理是否在子列表中,但您需要搜索代理是否在子列表的任何元素中。确定,另一个答案提供了更好的解决方案,然后感谢您的回答。如何优化此代码,以便只添加“代理”列表中的第一个匹配项?现在,如果我得到多个匹配项,它将被追加。现在它返回null。对于没有匹配项的情况,如输出中的“[‘明胶’、‘45.6 mg’、‘14.5%’、‘明胶包括一定百分比的水’”,我如何将空字符串追加到输出中。示例:
[''‘明胶’、‘45.6 mg’、‘14.5%’、‘明胶包括一定百分比的水’]