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