擅长:python、mysql、java
<p>对于更具Python风格的方法,您还可以使用:</p>
<pre><code>"/tennis/qatar/atp-doha-2009/hernych-jan-monfils-gael-S8Lm3D4l/".split('/')[-2]
</code></pre>
<p><a href="https://docs.python.org/3/library/stdtypes.html#str.split" rel="nofollow noreferrer">str.split</a>输出使用提供的分隔符分割的单词列表(在本例中为<code>'/'</code>)。因此,为了打破上述说法</p>
<pre><code>s = "/tennis/qatar/atp-doha-2009/hernych-jan-monfils-gael-S8Lm3D4l/".split('/')
print(s)
>> ['', 'tennis', 'qatar', 'atp-doha-2009', 'hernych-jan-monfils-gael-S8Lm3D4l', '']
print(s[-2])
>> 'hernych-jan-monfils-gael-S8Lm3D4l'
</code></pre>