使用numpy数组而不是python列表的缺点是什么?

2024-05-29 10:00:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个程序,其中我想展平一个数组,因此我使用了以下代码:

list_of_lists = [["a","b","c"], ["d","e","f"], ["g","h","i"]]
flattened_list = [i for j in list_of_lists for i in j]

这将产生['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'],即所需的输出。你知道吗

然后我发现使用numpy数组,我也可以通过使用np.array(((1,2),(3,4),(5,6))).flatten()来做同样的事情。你知道吗

我想知道总是用numpy数组来代替常规的Python列表是否有什么缺点?换句话说,Python列表是否可以做numpy数组不能做的事情?你知道吗


Tags: of代码in程序numpy列表fornp
3条回答

是的,有。经验法则是记住numpy.array对于相同数据类型的数据(所有整数、所有双精度fp、所有布尔值、相同长度的字符串等)比一堆东西更好。在后一种情况下,考虑到这一点,您最好使用generic list:

In [93]: a = [b'5', 5, '55', 'ab', 'cde', 'ef', 4, 6]

In [94]: b = np.array(a)

In [95]: %timeit 5 in a
65.6 ns ± 0.79 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [96]: %timeit 6 in a  # worst case
219 ns ± 5.48 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [97]: %timeit 5 in b
10.9 µs ± 217 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

看看这几个数量级的性能差异,其中numpy.array比较慢!当然,这取决于列表的维度,在这种特殊情况下取决于5或6的索引(O(n)复杂性的最坏情况),但是你明白了。你知道吗

Numpy数组和函数在大多数情况下更好。这里有一篇文章,如果你想进一步了解它:https://webcourses.ucf.edu/courses/1249560/pages/python-lists-vs-numpy-arrays-what-is-the-difference

在您的小示例中,列表理解比数组方法更快,即使将数组创建从计时循环中移除:

In [204]: list_of_lists = [["a","b","c"], ["d","e","f"], ["g","h","i"]] 
     ...: flattened_list = [i for j in list_of_lists for i in j]    

In [205]: timeit [i for j in list_of_lists for i in j]                                                       
757 ns ± 17.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [206]: np.ravel(list_of_lists)                                                                            
Out[206]: array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], dtype='<U1')

In [207]: timeit np.ravel(list_of_lists)                                                                     
8.05 µs ± 12.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

In [208]: %%timeit x = np.array(list_of_lists) 
     ...: np.ravel(x)                                                                                                     
2.33 µs ± 22.8 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

举一个更大的例子,我希望[208]会变得更好。你知道吗

如果子列表的大小不同,则数组不是二维的,展平不会执行任何操作:

In [209]: list_of_lists = [["a","b","c",23], ["d",None,"f"], ["g","h","i"]] 
     ...: flattened_list = [i for j in list_of_lists for i in j]                                             
In [210]: flattened_list                                                                                     
Out[210]: ['a', 'b', 'c', 23, 'd', None, 'f', 'g', 'h', 'i']
In [211]: np.array(list_of_lists)                                                                            
Out[211]: 
array([list(['a', 'b', 'c', 23]), list(['d', None, 'f']),
       list(['g', 'h', 'i'])], dtype=object)

增加列表更有效率:

In [217]: alist = []                                                                                         
In [218]: for row in list_of_lists: 
     ...:     alist.append(row) 
     ...:                                                                                                    
In [219]: alist                                                                                              
Out[219]: [['a', 'b', 23], ['d', None, 'f'], ['g', 'h', 'i']]
In [220]: np.array(alist)                                                                                    
Out[220]: 
array([['a', 'b', 23],
       ['d', None, 'f'],
       ['g', 'h', 'i']], dtype=object)

我们强烈反对迭代连接。首先收集列表中的子列表或数组。你知道吗

相关问题 更多 >

    热门问题