Module introduction
The array module in the Python standard library provides a fixed-type array class for efficiently storing elements of the same type.
Compared to the built-in list type, array is more memory-saving and capable of handling large amounts of data. array is mainly used for numerical calculations and processing of large-scale data of the same type. Because its internal storage is more compact than lists, array can improve program performance in some scenarios, especially when it involves a large number of numerical operations.
Use scenarios
- Numerical calculation: If you need to store a large amount of numerical data and the element types are the same, array is a good choice. It stores data in a fixed-size format in memory and is suitable for numerical calculations.
- Optimize memory: For cases where only simple data types (such as integers or floating point numbers) are needed, array is more memory-saving than list.
- File Operation:array supports direct interaction with binary files, reading or writing data to files in binary format, suitable for storage and reading of large-scale data.
Main categories
kind | describe |
---|---|
array | Used to create a fixed-type array object, the array element types are consistent and support efficient memory storage. |
- array
parameter:
-
typecode: One character, indicatingTypes of elements in arrays. Common type codes are:
- b: Signed characters (byte)
- B: Unsigned characters
- u: Unicode characters (Python 3.3 is deprecated)
- h: Signed short integer
- H: Unsigned short
- i: Signed integer
- I:Unsigned integer
- l: Signed long shape
- L: Unsigned long
- f:Floating point type
- d: Double precision floating point type
- initializer: Optional, used to initialize the contents of an array, which can be a list, tuple, or other iterable object.
import array # Create an array containing integersarr_int = ('i', [1, 2, 3, 4, 5]) print(arr_int) # Output: array('i', [1, 2, 3, 4, 5]) # Create an array containing floating point numbersarr_float = ('f', [1.1, 2.2, 3.3]) print(arr_float) # Output: array('f', [1.100000023841858, 2.20000047683716, 3.299999952316284])
Main functions
function | describe | Example |
---|---|---|
(x) | Add an element x to the end of the array. | (5) Add 5 to the end of the array. |
(i, x) | Insert element x at the specified index i. | (1, 10) Insert 10 at index 1. |
(x) | Removes the first matching element x in the array. | (2) Remove the first 2 that appears. |
([i]) | Removes and returns the element at index i. If not specified, the last element is deleted by default. | (1) Delete and return the element at index 1. |
(iter) | Adds all elements in iterable object to the array. | ([6, 7]) Add [6, 7] to the end of the array. |
() | Inverts the order of elements in the array. | () Inverts elements in the array. |
(file) | Writes elements of the array to the specified file. | (f) Write the array to file f. |
(file, count) | Read count elements from the file and save them into an array. | (f, 4) Read 4 elements from file f. |
(x) | Returns the index of the first occurrence of element x in the array. | (3) Returns the index of element 3. |
(x) | Returns the number of times element x appears in the array. | (3) Returns the number of times element 3 appears. |
- append()
Add elements
#Add elementsarr_int.append(6) print(arr_int) # Output: array('i', [1, 2, 3, 4, 5, 6])
- insert()
Insert element
# Insert elementarr_int.insert(2, 10) # Insert 10 at index 2print(arr_int) # Output: array('i', [1, 2, 10, 3, 4, 5, 6])
- remove()
Delete elements
# Delete elementsarr_int.remove(4) # Delete the first 4print(arr_int) # Output: array('i', [1, 2, 10, 3, 5, 6])
- pop()
Popup elements
# Pop up elementspopped = arr_int.pop(3) # Elements at index 3 pop-upprint(popped) # Output: 3print(arr_int) # Output: array('i', [1, 2, 10, 5, 6])
- reverse()
Invert the array
# Invert the arrayarr_int.reverse() print(arr_int) # Output: array('i', [6, 5, 10, 2, 1])
- count()
Calculate the number of occurrences of elements
# Calculate the number of elements appearingprint(arr_int.count(10)) # Output: 1
- index()
Find element index
# Find element indexprint(arr_int.index(5)) # Output: 1
- open()
Read and write files
# Write to a filearr_int = ('i', [1, 2, 3, 4, 5]) with open('array_data.bin', 'wb') as f: arr_int.tofile(f) # Read from a filearr_new = ('i', [0, 0, 0, 0, 0]) with open('array_data.bin', 'rb') as f: arr_new.fromfile(f, 5) print(arr_new) # Output: array('i', [1, 2, 3, 4, 5])
Things to note
Type Limitations: The element types in array must be consistent, andOnly basic data types can be stored(such as integers, floating point numbers, etc.). If you need to store different types of data, you can use a list.
Type code: When creating an array,A valid type code must be specified(For example, ‘i’ represents an integer and ‘f’ represents a floating point number). Different types of elements cannot be mixed in the same array.
Memory optimization:array is more memory-saving than list, especially when dealing with large amounts of numerical data. If you only need to store numerical data, array is a more efficient choice than list.
Operational efficiency:array is mainly used to efficiently store and process large amounts of data, especially suitable for numerical calculations and binary interaction with files, but is not as flexible as list, especially when it is necessary to process multiple different data types.
Complex types are not supported:array Not supported to store complex types(such as dictionaries, lists, etc.), only basic data types can be stored.
Summarize
The array module in Python provides an efficient way to store and process data of the same type. Compared with Python lists, array is more cost-effective in memory usage, and is especially suitable for scenarios where numerical calculations and processing large amounts of data are used. It ensures efficient storage by forcing element type consistency, but it also limits its flexibility. Array is an ideal choice for applications that need to process large amounts of numerical data or need to interact with binary files.