擅长:python、mysql、java
<p>在<a href="https://docs.python.org/3/library/re.html#re.findall" rel="nofollow noreferrer">doc</a>中声明包含空匹配。在</p>
<blockquote>
<p>If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result.</p>
</blockquote>
<p>这意味着你需要自己过滤掉空的化合物。使用空字符串的错误。在</p>
<pre><code>import re
name = 'propane'
alkanes = re.findall(r'(\d+\W+)*(methyl|ethyl|propyl|butyl)*(meth|eth|prop|but|pent|hex)(ane)', name)
alkanes = [tuple(comp for comp in a if comp) for a in alkanes]
print(alkanes) # [('prop', 'ane')]
</code></pre>
<p>另外,避免使用大写的变量名,因为它们通常是为类名保留的。在</p>