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 :
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
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: 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 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]
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]