Python 列表中dicts from List的flatteing值的列表理解

Python 列表中dicts from List的flatteing值的列表理解,python,ruby,arrays,Python,Ruby,Arrays,我想从dict列表的几个键中得到一个一维值列表 我在Ruby中就是这样做的: irb> list_ = [{a:1, b:2, c:3}, {a:4, b:5, c:6}] irb> list_.flat_map{ |dict_| dict_.values_at :b, :c } => [2, 3, 5, 6] 现在我如何在Python中实现它?您可以使用itertools.chain: >>> from itertools import chain,im

我想从dict列表的几个键中得到一个一维值列表

我在Ruby中就是这样做的:

irb> list_ = [{a:1, b:2, c:3}, {a:4, b:5, c:6}]

irb> list_.flat_map{ |dict_| dict_.values_at :b, :c }
=> [2, 3, 5, 6]

现在我如何在Python中实现它?

您可以使用
itertools.chain

>>> from itertools import chain,imap

>>> lis = [{'a':1, 'b':2, 'c':3}, {'a':4, 'b':5, 'c':6}]

>>> list(chain.from_iterable((x[y] for y in ('b','c')) for x in lis))
[2, 3, 5, 6]
或列表理解版本(内存效率较低):

根据建议,您还可以将
operator.itemgetter
itertools.imap
itertools.chain
一起使用,这比所使用的更快

时间:

>>> lis = [{'a':1, 'b':2, 'c':3}, {'a':4, 'b':5, 'c':6}]*10**5

>>> %timeit list(chain.from_iterable((x[y] for y in ('b','c')) for x in lis))
1 loops, best of 3: 276 ms per loop

>>> %timeit list(chain.from_iterable([x[y] for y in ('b','c')] for x in lis))
1 loops, best of 3: 183 ms per loop

>>> %timeit list(chain.from_iterable(imap(itemgetter('b', 'c'), lis))) #winner
10 loops, best of 3: 74.6 ms per loop

>>> %timeit [dct[i] for dct in lis for i in ('b', 'c')]  
10 loops, best of 3: 98.4 ms per loop

您可以使用
itertools.chain

>>> from itertools import chain,imap

>>> lis = [{'a':1, 'b':2, 'c':3}, {'a':4, 'b':5, 'c':6}]

>>> list(chain.from_iterable((x[y] for y in ('b','c')) for x in lis))
[2, 3, 5, 6]
或列表理解版本(内存效率较低):

根据建议,您还可以将
operator.itemgetter
itertools.imap
itertools.chain
一起使用,这比所使用的更快

时间:

>>> lis = [{'a':1, 'b':2, 'c':3}, {'a':4, 'b':5, 'c':6}]*10**5

>>> %timeit list(chain.from_iterable((x[y] for y in ('b','c')) for x in lis))
1 loops, best of 3: 276 ms per loop

>>> %timeit list(chain.from_iterable([x[y] for y in ('b','c')] for x in lis))
1 loops, best of 3: 183 ms per loop

>>> %timeit list(chain.from_iterable(imap(itemgetter('b', 'c'), lis))) #winner
10 loops, best of 3: 74.6 ms per loop

>>> %timeit [dct[i] for dct in lis for i in ('b', 'c')]  
10 loops, best of 3: 98.4 ms per loop

我会这样做:

>>> lst = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}]
>>> [dct[i] for dct in lst for i in ('b', 'c')]
[2, 3, 5, 6]

我会这样做:

>>> lst = [{'a': 1, 'b': 2, 'c': 3}, {'a': 4, 'b': 5, 'c': 6}]
>>> [dct[i] for dct in lst for i in ('b', 'c')]
[2, 3, 5, 6]

+1这是优雅的,速度几乎是我的解决方案的两倍。+1这是优雅的,速度几乎是我的解决方案的两倍。您可以从生成器1中删除不必要的支架,它们没有任何作用。还可以尝试计时
list(chain.from_iterable(imap(itemgetter('b','c'),lst))
它非常接近list comp(实际上在我的系统上比
1ms
快。@jamylak它甚至比LC版本更快。我还应该提到,我对
map
imap
进行了快速编辑,也尝试一下;)@jamylak你应该把它作为你自己的答案发布这只是你的一个轻微的变化,只是为了检查速度,我不推荐它作为一个解决方案,虽然你可以删除不必要的支架从发电机一,他们没有任何用途。还可以尝试计时
list(chain.from_iterable(imap(itemgetter('b','c'),lst))
它非常接近list comp(实际上在我的系统上比
1ms
快。@jamylak它甚至比LC版本更快。我还应该提到,我对
map
imap
进行了快速编辑,也尝试一下;)@jamylak你应该把它作为你自己的答案发布这只是你的一个微小的变化,只是为了检查速度,我不推荐它作为解决方案though@Charles,让我问一下:那个标签怎么了?为什么我看到了它的“4个订户”,但不存在任何问题。
one liner
是一个问题。标记订阅在标记删除后仍然有效。你不会相信有那么多人订阅了我的“永恒的复仇女神”标签,
server
@Charles,让我问一下:这个标签怎么了?为什么我看到了它的“4个订户”,但不存在任何问题。
one liner
是一个问题。标记订阅在标记删除后仍然有效。你不会相信有那么多人订阅了我永恒的复仇女神标签,
server