初步提示:测试代码中,右尖括号(>)表示命令行输入的命令;输出内容是以井号(#)开头的单行;该库的导入仅在本文第一个测试代码中显示,其他代码块中省略了该库的导入代码。系统类型:Windows10python版本:Python3.9.0array模块定义了一个对象类型,可以简洁地表示由字符、整数、浮点数等基本类型组成的数组。array模块中定义的数组属于序列类型,其行为与列表类型非常相似,但数组中元素的数据类型有限制,只能设置初始化时指定的一种类型。++PS:数组类似于列表、元组等复合数据类型,可以由多个值组成。出于个人习惯,小编把这些值称为元素,也就是说,列表、元组等可以由多个元素组成,那些组成数组的值在本文中也称为元素。使用++array模块时,需要实例化一个数组对象才能进行下一步:array.array(typecode[,initializer])'''参数:typecode:指定当前数组可以接受的元素数据类型initializer:可选参数,数组初始化时加入的元素,必须是可迭代对象,元素的数据类型受参数typecode'''typecode参数的值是一个字符,这个字符称为a类型代码,代表一个类型限制,所有的类型代码都可以使用array.typecodes查看:代码是什么意思?类型代码C类型Python类型最小字节大小'b'signedcharint1'B'unsignedcharint1'u'wchar_tUnicodecharacter2'h'signedshortint2'H'unsignedshortint2'i'signedintint2'I'unsignedint2'l'signedlongint4'L'unsignedlongint4'q'signedlonglongint8'Q'unsignedlonglongint8'f'floatfloat4'd'doublefloat8接下来,你可以初始化一个数组:test=array.array('u','ABC')print(test)#array('u','ABC')初始化的元素类型必须和设置的类型码一致,否则会报错:test=array.array('b','ABC')#TypeError:cannotuseastrtoinitializeanarraywithtypecode'b'arraymodule数组的大部分内容是在初始化的数组对象上展开的,所以下面会按照功能分组,引入属性array.typecode:获取数组的类型码array.itemsize:获取字节长度ofanelementintheinternalrepresentationtest=array.array('u','ABC')print(test.typecode)#uprint(test.itemsize)#2添加和添加函数比较统一,没有返回值,它直接作用于数组本身。array.append(x)将值为x的新元素追加到数组末尾。参数x必须是符合类型代码的值。array.extend(iterable)将iterable中的元素添加到数组的末尾。iterable可迭代对象中的所有元素都符合类型代码。array.fromlist(list)将列表中的元素添加到数组的末尾。列表中的所有元素都符合类型代码。array.fromunicode(s)将Unicode字符串添加到数组的末尾。数组的类型码必须是u,否则会出现ValueError。array.insert(i,x)在数组中的索引i处插入参数x。如果指定的参数i为负数,则将元素x插入到数组的末尾。参数x必须是符合类型代码的值。test=array.array('u','A')'''append()'''test.append('B')print(test)#array('u','AB')'''扩展()'''test.extend(['C','D'])print(test)#array('u','ABCD')'''fromlist()'''test.fromlist(['E','F'])print(test)#array('u','ABCDEF')'''fromunicode()'''test.fromunicode('IJ')print(test)#array('u','ABCDEFIJ')'''insert()'''test.insert(6,'H')print(test)#array('u','ABCDEFHIJ')统计并getarray.count(x)getvalue的x的元素数。参数x必须是符合类型代码的值。array.index(x)获取第一个值为x的元素的位置,即索引值。参数x必须是符合类型代码的值。array.pop([i])从数组中移除并返回指定索引值的元素,默认移除并返回数组中的最后一个元素。test=array.array('u','ABCABC')'''count()'''print(test.count('A'))#2'''index()'''print(test.index('B'))#1'''pop()'''print(test.pop())#Cprint(test)#array('u','ABCAB')机器值,文件相关array.frombytes(s)解释完二进制字符串后,将其添加到数组的末尾。array.fromfile(f,n)从文件对象f中读取n个元素并将它们附加到数组的末尾。如果可读数据小于参数n,会报EOFError错误,但仍会向数组中添加有效元素。参数f必须是内置文件对象。array.tobytes()将数组转换为机器值并返回。array.tofile(f)将数组转换为机器值并写入文件。参数f必须是内置文件对象。array.byteswap()交换数组中所有元素的字节。该方法只支持大小为1、2、4、8字节的值。其他值会报RuntimeError错误。test=array.array('u','ABC')'''tobytes()'''print(test.tobytes())#b'A\x00B\x00C\x00''''tofile()'''withopen('./test','wb+')asf:test.tofile(f)f.seek(0)#将指针移动到文件开头print(f.read())#读取文件内容#b'A\x00B\x00C\x00''''frombytes()'''test.frombytes(b'A\x00B\x00C\x00')print(test)#array('u','ABCABC')'''fromfile()'''withopen('./test','rb+')asf:test.fromfile(f,3)print(test)#array('u','ABCABCABC')'''byteswap()'''test=array.array('u','ABC')test.byteswap()print(test)#array('u','以什吾')转换array.tolist()willConvertanarraytoalist.array.tounicode()将数组转换为Unicode字符串,数组的类型编码为u,否则会报ValueError。test=array.array('u','ABC')'''tolist()'''print(test.tolist())#['A','B','C']'''tounicode()'''print(test.tounicode())#ABCotherarray.buffer_info()返回一个元组(address,length)给出当前内存地址和用于存储数组内容的buffer元素的长度。array.remove(x)从数组中移除值为x的第一个元素。参数x必须是符合类型代码的值。array.reverse()反转数组中元素的顺序。test=array.array('u','ABCABC')'''buffer_info()'''print(test.buffer_info())#(1864606516176,6)'''remove()'''test.remove('C')print(test)#array('u','ABABC')'''reverse()'''test.reverse()print(test)#array('u','CBABA')公众号:《python杂货店》,重点讲解python语言及相关知识。发现更多原创文章,期待您的关注。参考官方文档:https://docs.python.org/zh-cn...
