Arrays in python (Array)
In Python, an array (Array) is an ordered collection of data used to store a fixed number of elements of the same type. An array is a contiguous memory space where each element can be accessed and modified by index.
Features:
- The elements in an array have the same data type, which can be numbers, strings, or other types.
- The size of the array is fixed, once created, its length cannot be changed.
- The elements in the array can be accessed and modified by index value.
- The elements in the array are stored contiguously in memory.
Create an array:
In Python, you can use third-party libraries numpy
to create and manipulate arrays. Numpy is a powerful mathematical and scientific computing library for Python, which provides a wealth of functions and methods for efficiently manipulating multidimensional arrays.
First you need to install numpy
the library , you can use the following command to install:
pip install numpy
Once installed, you can use numpy
to create arrays:
import numpy as np
arr = np.array([1, 2, 3, 4, 5]) # 创建一维数组
print(arr) # 输出: [1 2 3 4 5]
matrix = np.array([[1, 2, 3], [4, 5, 6]]) # 创建二维数组
print(matrix)
# 输出:
# [[1 2 3]
# [4 5 6]]
Access and modify array elements:
An index value can be used to access a specific element in an array. Index values start at 0 and can be integers or slice objects. For multidimensional arrays, elements can be accessed and modified by indexing layer by layer.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[0]) # 输出: 1,访问第一个元素
arr[2] = 10 # 修改第三个元素为10
print(arr) # 输出: [ 1 2 10 4 5]
matrix = np.array([[1, 2, 3], [4, 5, 6]])
print(matrix[0, 1]) # 输出: 2,访问第一行第二列元素
matrix[1, 2] = 7 # 修改第二行第三列元素为7
print(matrix)
# 输出:
# [[1 2 3]
# [4 5 7]]
Common operations:
- Array shape: You can use
shape
the attribute to get the shape of the array, and return a tuple representing the size of each dimension.
import numpy as np
matrix = np.array([[1, 2, 3], [4, 5, 6]])
shape = matrix.shape
print(shape) # 输出: (2, 3),表示2行3列的二维数组
- Array operations: Numpy provides a wealth of functions and methods to manipulate arrays, such as calculating the maximum, minimum, average, and sorting.
import numpy as np
arr = np.array([5, 2, 1, 6, 4])
maximum = np.max(arr) # 计算数组的最大值
print(maximum) # 输出: 6
minimum = np.min(arr) # 计算数组的最小值
print(minimum) # 输出: 1
mean = np.mean(arr) # 计算数组的平均值
print(mean) # 输出: 3.6
sorted_arr = np.sort(arr) # 对数组进行排序
print(sorted_arr) # 输出: [1 2 4 5 6]
- Array slicing: A slice object can be used to obtain a subset of an array. A slice object consists of a start index, an end index, and a step size.
import numpy as np
arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
subset = arr[2:6] # 获取索引2到5(不包括6)的子集
print(subset) # 输出: [2 3 4 5]
reversed_arr = arr[::-1] # 将数组逆序
print(reversed_arr) # 输出: [9 8 7 6 5 4 3 2 1 0]
The above is a detailed explanation about arrays in Python. Arrays are a common data structure used to store and manipulate large amounts of data of the same type. With the help of third-party libraries numpy
, we can efficiently create, access and manipulate arrays, so that we can conveniently perform numerical calculations and scientific operations.