Python 简明教程

Python - Array Methods

Python 中的 array 模块为表示基本值的数组(如字符、整数和浮点数)提供了一个高效的对象类型。数组类似于列表,但它按顺序存储同类数据元素。在创建数组时,使用单个字符类型代码指定类型。

The array module in Python offers an efficient object type for representing arrays of basic values like characters, integers, and floating point numbers. Arrays are similar to lists but it stores a collection of homogeneous data elements in order. At the time of creating array’s type is specified using a single character type code.

数组方法提供各种数组对象操作,包括追加、扩展和操作元素。这些方法用于有效处理同质的基本数据类型集合,使其适用于需要紧凑数据存储的任务,例如数字计算。

Array methods provides various operations on array objects, including appending, extending, and manipulating elements. These methods are used for efficient handling of homogeneous collections of basic data types, making them suitable for tasks requiring compact data storage, such as numerical computations.

Python Array Class

array 类定义了多种方法,包括增加和删除元素、获取数组信息、操作数组元素、以及在数组和其他数据类型之间转换。下面按其功能对方法进行分类。我们来探索和理解每种方法的功能。

The array class defines several methods, including adding and removing elements, obtaining information about the array, manipulating array elements, and converting arrays to and from other data types. Below are categorized methods based on their functionality. Let’s explore and understand the functionality of each method.

数组使用 array.array(typecode[, initializer]) 类创建,其中,类型代码是一个单字符,它定义了数组中元素的类型,初始化器是一个可选值,用于初始化数组。

Arrays are created using the array.array(typecode[, initializer]) class, where typecode is a single character that defines the type of elements in the array, and initializer is an optional value used to initialize the array.

Adding and Removing Elements

以下方法用于追加、扩展、插入和从数组中删除元素 -

Below methods are used for appending, extending, inserting, and removing elements from arrays −

Sr.No.

Methods with Description

1

append(x) Appends a new item with value x to the end of the array.

2

extend(iterable) Appends items from iterable to the end of the array.

3

insert(i, x) Inserts a new item with value x before position i.

4

pop([i]) Removes and returns the item with index i. If i is not specified, removes and returns the last item.

5

remove(x) Removes the first occurrence of x from the array.

Information and Utility Methods

这些方法用于获取有关数组的信息并执行实用操作 −

These methods are used for obtaining information about arrays and to perform utility operations −

Sr.No.

Methods with Description

1

buffer_info() Returns a tuple (address, length) giving the current memory address and the length in elements of the buffer used to hold the array’s contents.

2

count(x) Returns the number of occurrences of x in the array.

3

index(x[, start[, stop]]) Returns the smallest index where x is found in the array. Optional start and stop arguments can specify a sub-range to search.

Manipulating Array Elements

以下是用于操作数组元素的方法,例如反向数组或字节交换值。

Following methods are used for manipulating array elements, such as reversing the array or byteswapping values.

Sr.No.

Methods with Description

1

reverse() Reverses the order of the items in the array.

2

byteswap() "Byteswaps" all items of the array, useful for reading data from a file written on a machine with a different byte order.

Conversion Methods

这些方法用于在数组、文件、列表和 Unicode 字符串之间进行转换。

These methods are used to convert arrays to and from bytes, files, lists, and Unicode strings.

Sr.No.

Methods with Description

1

frombytes(buffer) Appends items from the bytes-like object, interpreting its content as an array of machine values.

2

tobytes() Converts the array to a bytes representation.

3

fromfile(f, n) Reads n items from the file object f and appends them to the array.

4

tofile(f) Writes all items to the file object f.

5

fromlist(list) Appends items from the list to the array.

6

tolist() Converts the array to a list with the same items.

7

fromunicode(s) Extends the array with data from the given Unicode string. The array must have type code 'u'.

8

tounicode() Converts the array to a Unicode string. The array must have type code 'u'.