5-6 高效率的array

import array

a = array.array("B")
a.append(1)
a.append(2)
print(a)
print(a.tostring())
array('B', [1, 2])
b'\x01\x02'
array = ArrayType

class ArrayType(object):
    """
    array(typecode [, initializer]) -> array
    
    Return a new array whose items are restricted by typecode, and
    initialized from the optional initializer value, which must be a list,
    string or iterable over elements of the appropriate type.
    
    Arrays represent basic values and behave very much like lists, except
    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             Minimum size in bytes 
        'b'         signed integer     1 
        'B'         unsigned integer   1 
        'u'         Unicode character  2 (see note) 
        'h'         signed integer     2 
        'H'         unsigned integer   2 
        'i'         signed integer     2 
        'I'         unsigned integer   2 
        'l'         signed integer     4 
        'L'         unsigned integer   4 
        'q'         signed integer     8 (see note) 
        'Q'         unsigned integer   8 (see note) 
        'f'         floating point     4 
        'd'         floating point     8 
        """

array的初始化需要指定数据类型,它不像list可以存放任意类型数据,可以混合存放;
array只可以存放一种数据,并且初始化时要说明
‘b’ 指的是有符号的整形

你可能感兴趣的:(5-6 高效率的array)