简体   繁体   中英

Differences between python's numpy.ndarray and list datatypes

What are the differences between python's numpy.ndarray and list datatypes? 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.
  • Lists can containt about everything, in numpy arrays all the elements must have the same type.
  • In practice, numpy arrays are faster for vectorial functions than mapping functions to lists.
  • 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).

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).

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.

  2. One of the main advantages of NumPy is its advantage in time compared to standard Python.

Good Explanation : https://www.python-course.eu/numpy.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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