Python 从列表理解和一般情况下有效地创建 numpy 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14372613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Efficient creation of numpy arrays from list comprehension and in general
提问by NielsGM
In my current work i use Numpy and list comprehensions a lot and in the interest of best possible performance i have the following questions:
在我目前的工作中,我经常使用 Numpy 和列表推导式,为了获得最佳性能,我有以下问题:
What actually happens behind the scenes if i create a Numpy array as follows? :
如果我按如下方式创建 Numpy 数组,幕后实际会发生什么?:
a = numpy.array( [1,2,3,4] )
My guess is that python first creates an ordinary list containing the values, then uses the list size to allocate a numpy array and afterwards copies the values into this new array. Is this correct, or is the interpreter clever enough to realize that the list is only intermediary and instead copy the values directly?
我的猜测是 python 首先创建一个包含值的普通列表,然后使用列表大小分配一个 numpy 数组,然后将值复制到这个新数组中。这是正确的,还是解释器足够聪明以意识到列表只是中间的,而是直接复制值?
Similarly, if i wish to create a numpy array from list comprehension using numpy.fromiter():
同样,如果我想使用 numpy.fromiter() 从列表推导中创建一个 numpy 数组:
a = numpy.fromiter( [ x for x in xrange(0,4) ], int )
will this result in an intermediary list of values being created before being fed into fromiter()?
这会导致在被送入 fromiter() 之前创建一个中间值列表吗?
Best regards Niels
最好的问候尼尔斯
采纳答案by Snakes and Coffee
I believe than answer you are looking for is using generator expressionswith numpy.fromiter.
我相信您正在寻找的答案是generator expressions与numpy.fromiter 一起使用。
numpy.fromiter((<some_func>(x) for x in <something>),<dtype>,<size of something>)
Generator expressions are lazy - they evaluate the expression when you iterate through them.
生成器表达式是惰性的——当你遍历它们时,它们会评估表达式。
Using list comprehensions makes the list, then feeds it into numpy, while generator expressions will yield one at a time.
使用列表推导式生成列表,然后将其输入 numpy,而生成器表达式将一次生成一个。
Python evaluates things inside -> out, like most languages (if not all), so using [<something> for <something_else> in <something_different>]would make the list, then iterate over it.
Python 会从内到外评估事物,就像大多数语言(如果不是全部)一样,因此使用[<something> for <something_else> in <something_different>]会生成列表,然后对其进行迭代。
回答by wim
You could create your own list and experiment with it to shed some light on the situation...
您可以创建自己的列表并对其进行试验以了解情况...
>>> class my_list(list):
... def __init__(self, arg):
... print 'spam'
... super(my_list, self).__init__(arg)
... def __len__(self):
... print 'eggs'
... return super(my_list, self).__len__()
...
>>> x = my_list([0,1,2,3])
spam
>>> len(x)
eggs
4
>>> import numpy as np
>>> np.array(x)
eggs
eggs
eggs
eggs
array([0, 1, 2, 3])
>>> np.fromiter(x, int)
array([0, 1, 2, 3])
>>> np.array(my_list([0,1,2,3]))
spam
eggs
eggs
eggs
eggs
array([0, 1, 2, 3])

