程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python playful data 6 - the difference between numpy ndarray array and python list

編輯:Python

introduction

The first two articles introduce Python Dynamic type Shared reference , The principle of assignment and copying , Yes Python list The list should have a deep understanding . that NumPy Medium ndarray Array and list What's the difference between lists . more Python Advanced Series , Please refer to Python Advanced learning Play data series

Summary :

  1. Python Different types of memory requirements in
  2. NumPy ndarray Vs. Python list Memory requirements
  3. NumPy ndarray Vs. Python list structure
  4. NumPy ndarray Vs. Python list Calculation
  5. NumPy subarray Vs. Python sublist
  6. NumPy ndarray Vs. Python list summary

Python Different types of memory requirements in

A variable is assigned different types of values , Its storage requirements in memory are also different . as follows :

from sys import getsizeof as byte_size
x = 5
print("byte size of int_type:{}".format(byte_size(x)))
x = 'hello'
print("byte size of str_type:{}".format(byte_size(x)))
x = True
print("byte size of bool_type:{}".format(byte_size(x)))
x = 5.0
print("byte size of float_type:{}".format(byte_size(x)))

Output :

byte size of int_type:28
byte size of str_type:54
byte size of bool_type:28
byte size of float_type:24

list The amount of memory used by each element in depends on the type of each element , as follows :

from sys import getsizeof as byte_size
list_object = ['Five', 5, 5.0, True]
print("byte size of list_object:{}".format(byte_size(list_object)))
print("byte size of each list item in list_object:{}".format([byte_size(item) for item in list_object]))
print("total byte size of list items in list_object:{}".format(sum([byte_size(item) for item in list_object])))

Output :

byte size of list_object:96
byte size of each list item in list_object:[53, 28, 24, 28]
byte total size of list items in list_object:133

NumPy ndarray Vs. Python list Memory requirements

Let's look at the code first :

from sys import getsizeof as byte_size
import numpy as np
list_object = [1,2,3,4,5,6]
np_array = np.array(list_object, dtype='int16')
print("list_object:{}".format(list_object))
print("byte size of list_object:{}".format(byte_size(list_object)))
print("byte size of each list item in list_object:{}".format([byte_size(item) for item in list_object]))
print("total byte size of list items in list_object:{}\n".format(sum([byte_size(item) for item in list_object])))
print("np_array:{}".format(np_array))
print("byte size of each item in np_array:{}".format([item.nbytes for item in np_array]))
print("total byte size of items in np_array:{}".format(np_array.nbytes))

Output :
so NumPy array Arrays require less memory

list_object:[1, 2, 3, 4, 5, 6]
byte size of list_object:112
byte size of each list item in list_object:[28, 28, 28, 28, 28, 28]
total byte size of list items in list_object:168
np_array:[1 2 3 4 5 6]
byte size of each item in np_array:[2, 2, 2, 2, 2, 2]
total byte size of items in np_array:12

NumPy ndarray Vs. Python list structure :

NumPy ndarray: It stores the values of the elements in the array

A point to data ( A piece of data in a memory or memory mapped file ) The pointer to .
Data type or dtype, A cell that describes a fixed size value in an array .
An array shape (shape) tuples , A tuple representing the size of each dimension .
A span tuple (stride), The integer refers to the need to move forward to the next element of the current dimension " Across " Bytes of .


Python list: What's stored is list Reference to element , Memory address , It's not worth it

NumPy ndarray Vs. Python list Calculation :

NumPy ndarry It can perform arithmetic operations between data .
Python list No direct arithmetic operation is allowed between

import numpy as np
list_object = [1,2,3,4,5,6]
np_array = np.array(list_object, dtype='int16')
list_object_plus= list_object + list_object
np_array_plus = np_array + np_array
np_array_minus = np_array - np_array
np_array_divide = np_array / np_array
np_array_multi = np_array * np_array
print("list_object:{}".format(list_object))
print("list_object + list_object:{}\n".format(list_object_plus))
print("np_array:{}".format(np_array))
print("np_array + np_array:{}".format(np_array_plus))
print("np_array - np_array:{}".format(np_array_minus))
print("np_array * np_array:{}".format(np_array_divide))
print("np_array / np_array:{}".format(np_array_multi))

Output :

list_object:[1, 2, 3, 4, 5, 6]
list_object + list_object:[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
np_array:[1 2 3 4 5 6]
np_array + np_array:[ 2 4 6 8 10 12]
np_array - np_array:[0 0 0 0 0 0]
np_array * np_array:[1. 1. 1. 1. 1. 1.]
np_array / np_array:[ 1 4 9 16 25 36]

NumPy subarray Vs. Python sublist

Let's take a look at subarray and sublist Different

import numpy as np
list_object = [1,2,3,4,5,6]
np_array = np.array(list_object, dtype='int16')
list_object_sub = list_object[0:4]
np_array_sub = np_array[0:4]
print("origin list_object:{}".format(list_object))
print("origin np_array:{}".format(np_array))
print("origin list_object_sub:{}".format(list_object_sub))
print("origin np_array_sub:{}".format(np_array_sub))
list_object_sub[0] = 0
np_array_sub[0] = 0
print("after changed list_object_sub:{}".format(list_object_sub))
print("after changed np_array_sub:{}".format(np_array_sub))
print("list_object:{}".format(list_object))
print("np_array:{}".format(np_array))

Output :
It can be seen that NumPy subarray It is an original array A view of , Its change will affect the original array. and Python sublist In fact, it's the original list A shallow copy, Its changes will not affect the original list.

origin list_object:[1, 2, 3, 4, 5, 6]
origin np_array:[1 2 3 4 5 6]
origin list_object_sub:[1, 2, 3, 4]
origin np_array_sub:[1 2 3 4]
after changed list_object_sub:[0, 2, 3, 4]
after changed np_array_sub:[0 2 3 4]
list_object:[1, 2, 3, 4, 5, 6]
np_array:[0 2 3 4 5 6]

NumPy ndarray Vs. Python list summary :

NumPy ArrayPython List Creation time size establish array when ,size Is constant , Once changed size, The original array Will be deleted , Recreate a new arraylist Of size Can change dynamically storage Continuous memory space Discontinuous memory space item Memory Every item Same amount of memory Every item The memory occupied can be different item type Every item The type must be the same Every item The types can be different Overall memory Less memory More memory is used to support dynamic type Vector calculation Directly support a large number of mathematical calculations Not directly supported
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved