Efficient creation of numpy arrays from list comprehension and in general

前端 未结 2 497
青春惊慌失措
青春惊慌失措 2020-12-08 04:14

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:

What actually happens b

相关标签:
2条回答
  • 2020-12-08 04:50

    I believe than answer you are looking for is using generator expressions with 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.

    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.

    0 讨论(0)
  • 2020-12-08 04:52

    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])
    
    0 讨论(0)
提交回复
热议问题