Efficient creation of numpy arrays from list comprehension and in general

前端 未结 2 506
青春惊慌失措
青春惊慌失措 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: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])
    

提交回复
热议问题