擅长:python、mysql、java
<p>下面是使用map()和lambda函数的另一种方法:</p>
<pre><code>>>> map(lambda d: d['value'], l)
</code></pre>
<p>我是名单上的人。
我认为这种方式“最性感”,但我会用列表理解。</p>
<p>更新:
如果“value”可能作为密钥使用丢失:</p>
<pre><code>>>> map(lambda d: d.get('value', 'default value'), l)
</code></pre>
<p>更新:我也不是兰巴斯的忠实粉丝,我更喜欢点名。。。我就是这么想的:</p>
<pre><code>>>> import operator
>>> map(operator.itemgetter('value'), l)
</code></pre>
<p>我甚至会更进一步,创建一个唯一的函数,明确地说明我想要实现的目标:</p>
<pre><code>>>> import operator, functools
>>> get_values = functools.partial(map, operator.itemgetter('value'))
>>> get_values(l)
... [<list of values>]
</code></pre>
<p>对于Python 3,由于<code>map</code>返回迭代器,因此使用<code>list</code>返回列表,例如<code>list(map(operator.itemgetter('value'), l))</code>。</p>