擅长:python、mysql、java
<blockquote>
<p>I am trying to write a program that orders a list of strings based on the last character in the item.</p>
</blockquote>
<pre><code>>>> s = ["Tiger 6", "Shark 4", "Cyborg 8"]
>>> sorted(s, key=lambda x: int(x[-1]))
['Shark 4', 'Tiger 6', 'Cyborg 8']
</code></pre>
<p>如果最后有更多的数字,请尝试此操作。</p>
<pre><code>>>> import re
>>> sorted(s, key=lambda x: int(re.search(r'\d+$',x).group()))
['Shark 4', 'Tiger 6', 'Cyborg 8']
</code></pre>
<p><code>re.search(r'\d+$',x).group()</code>有助于获取最后出现的数字,而不考虑前面的空格。</p>