擅长:python、mysql、java
<p>您可以在列表理解中使用<code>re.sub</code>。这样,在<code>nums</code>中的任何元素中处理<code>'Base Core'</code>的多次出现会更简单:</p>
<pre><code>import re
nums = ['Aero', 'Base Core Newton', 'Node']
new_nums = [re.sub('^Base(?=\sCore)', 'Fine', i) for i in nums]
</code></pre>
<p>输出:</p>
<pre><code>['Aero', 'Fine Core Newton', 'Node']
</code></pre>
<p><code>regex</code>说明:</p>
<pre><code>^ -> start of line anchor, anything proceeding must be at the start of the string
Base -> matches the "Base" in the string
?= -> positive lookahead, ^Base will not be matched unless the following pattern in parenthesis is found after ^Base
\sCore -> matches a single space, and then an occurrence of "Core"
</code></pre>