简体   繁体   English

python的numpy.ndarray和list数据类型之间的差异

[英]Differences between python's numpy.ndarray and list datatypes

What are the differences between python's numpy.ndarray and list datatypes? python的numpy.ndarray和list数据类型有什么区别? I have vague ideas, but would like to get a definitive answer about: 我有一些模糊的想法,但想得到一个明确的答案:

  1. Size in memory 内存大小
  2. Speed / order of access 访问速度/顺序
  3. Speed / order of modification in place but preserving length 修改的速度/顺序,但保留长度
  4. Effects of changing length 改变长度的影响

Thanks! 谢谢!

There are several differences: 有几个不同之处:

  • You can append elements to a list, but you can't change the size of a ´numpy.ndarray´ without making a full copy. 您可以将元素附加到列表中,但是如果不制作完整副本,则无法更改'numpy.ndarray'的大小。
  • Lists can containt about everything, in numpy arrays all the elements must have the same type. 列表可以包含所有内容,在numpy数组中,所有元素必须具有相同的类型。
  • In practice, numpy arrays are faster for vectorial functions than mapping functions to lists. 实际上,numpy数组比矢量函数更快,而不是将函数映射到列表。
  • I think than modification times is not an issue, but iteration over the elements is. 我认为修改时间不是问题,而是对元素的迭代。
  • Numpy arrays have many array related methods (´argmin´, ´min´, ´sort´, etc). Numpy数组有许多与数组相关的方法('argmin','min','orort'等)。

I prefer to use numpy arrays when I need to do some mathematical operations (sum, average, array multiplication, etc) and list when I need to iterate in 'items' (strings, files, etc). 当我需要进行一些数学运算(求和,平均,数组乘法等)时,我更喜欢使用numpy数组,当我需要迭代'items'(字符串,文件等)时,我更喜欢使用numpy数组。

I read from various links and resources and summarize my understanding as below: 我从各种链接和资源中读到并总结了我的理解如下:

  1. The main benefits of using numpy arrays should be smaller memory consumption and better runtime behavior. 使用numpy数组的主要好处应该是更小的内存消耗和更好的运行时行为。

  2. One of the main advantages of NumPy is its advantage in time compared to standard Python. 与标准Python相比,NumPy的主要优势之一是其优势。

Good Explanation : https://www.python-course.eu/numpy.php 好的解释: https//www.python-course.eu/numpy.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM