擅长:python、mysql、java
<p>我可能遗漏了问题的要点,但这里有一个正则表达式实现</p>
<pre><code>import re
# Sample data
disc = [' Disc - Standard Removal & Herbicide ',
' Disc - Standard Removal & Herbicide ',
' Standard Trim ',
' Disc - Hazard Tree',
' Disc - Hazard Tree ',]
# Regular Expression pattern
# We have Disc in parenthesis because that's what we want to capture.
# Using re.search(<pattern>, <string>).group(1) returns the first matching group. Using just
# re.search(<pattern>, <string>).group() would return the entire row.
disc_pattern = r"\s+?(Disc)\s+?"
# List comprehension that skips rows without 'Disc'
[re.search(disc_pattern, i).group(1) for i in disc if re.match(disc_pattern, i)]
</code></pre>
<p>输出:</p>
<pre><code>['Disc', 'Disc', 'Disc', 'Disc']
</code></pre>