<p>我认为你的问题是对网络等级的混淆,以及循环的顺序。假设您希望保留原始列表的顺序(而不是忽略元素),那么外部循环应该在列表上。然后您要检查列表中存在的<code>agent</code>中的任何单词。我们可以使用“flag”变量只添加一个“agent”:</p>
<pre class="lang-py prettyprint-override"><code>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)
</code></pre>
<p>提供:</p>
<pre><code>[['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']]
</code></pre>