在Python中,如果你有一个矩阵(通常使用NumPy库创建),你可以使用 `tolist()` 方法将其转换为列表。以下是两种常见情况的示例:
1. 将 `numpy.matrix` 对象转换为列表:
import numpy as np
x = np.matrix(np.arange(12).reshape((3,4)))
print(x.tolist())
输出:
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
2. 将 `numpy.ndarray` 对象转换为列表:
a = np.array([[1, 2], [3, 4]])
print(a.tolist())
输出:
[[1, 2], [3, 4]]
请注意,使用 `tolist()` 方法时,原始数组或矩阵可以被重新构造,因为列表是可变的,并且可以容纳不同类型的元素。