擅长:python、mysql、java
<p>方法<code>list.sort()</code>正在对列表进行就地排序,作为所有的变异方法,它返回<code>None</code>。使用内置函数<a href="http://docs.python.org/library/functions.html#sorted">^{<cd3>}</a>返回一个新的排序列表。</p>
<pre><code>result = sorted((trans for trans in my_list if trans.type in types),
key=lambda x: x.code)
</code></pre>
<p>代替<code>lambda x: x.code</code>,您还可以使用稍快的<code>operator.attrgetter("code")</code>。</p>