引子
在python中,存储序列数据(数组), 可以在list中存储。
但是list中元素可以支持不同类型的元素。这带来了数据存储的不规则性,但是现实中往往数组元素都是一致的。list处理上效率就会降低。
LIST
https://www.tutorialspoint.com/python/python_lists.htm
list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]
array.array
https://docs.python.org/3.5/library/array.html#module-array
array('l') array('u', 'hello u2641') array('l', [1, 2, 3, 4, 5]) array('d', [1.0, 2.0, 3.14])
Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. The type is specified at object creation time by using a type code, which is a single character. The following type codes are defined:
Type code C Type Python Type Minimum size in bytes Notes 'b'
signed char int 1 'B'
unsigned char int 1 'u'
Py_UNICODE Unicode character 2 (1) 'h'
signed short int 2 'H'
unsigned short int 2 'i'
signed int int 2 'I'
unsigned int int 2 'l'
signed long int 4 'L'
unsigned long int 4 'q'
signed long long int 8 (2) 'Q'
unsigned long long int 8 (2) 'f'
float float 4 'd'
double float 8
numpy.arrary
https://www.numpy.org.cn/user/quickstart.html#%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86
>>> import numpy as np >>> a = np.arange(15).reshape(3, 5) >>> a array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]) >>> a.shape (3, 5) >>> a.ndim 2 >>> a.dtype.name 'int64' >>> a.itemsize 8 >>> a.size 15 >>> type(a) <type 'numpy.ndarray'> >>> b = np.array([6, 7, 8]) >>> b array([6, 7, 8]) >>> type(b) <type 'numpy.ndarray'>
https://www.numpy.org.cn/user/basics/types.html#%E6%95%B0%E7%BB%84%E7%B1%BB%E5%9E%8B%E4%B9%8B%E9%97%B4%E7%9A%84%E8%BD%AC%E6%8D%A2
支持的原始类型与 C 中的原始类型紧密相关:
Numpy 的类型 C 的类型 描述 np.bool bool 存储为字节的布尔值(True或False) np.byte signed char 平台定义 np.ubyte unsigned char 平台定义 np.short short 平台定义 np.ushort unsigned short 平台定义 np.intc int 平台定义 np.uintc unsigned int 平台定义 np.int_ long 平台定义 np.uint unsigned long 平台定义 np.longlong long long 平台定义 np.ulonglong unsigned long long 平台定义 np.half / np.float16 半精度浮点数:符号位,5位指数,10位尾数 np.single float 平台定义的单精度浮点数:通常为符号位,8位指数,23位尾数 np.double double 平台定义的双精度浮点数:通常为符号位,11位指数,52位尾数。 np.longdouble long double 平台定义的扩展精度浮点数 np.csingle float complex 复数,由两个单精度浮点数(实部和虚部)表示 np.cdouble double complex 复数,由两个双精度浮点数(实部和虚部)表示。 np.clongdouble long double complex 复数,由两个扩展精度浮点数(实部和虚部)表示。