在Python中,向量可以通过多种方式表示,以下是几种常见的方法:
1. 使用Python列表(List):
vector = [1, 2, 3] 创建一个长度为3的向量
2. 使用NumPy库的数组(Array):
import numpy as np
v1 = np.array([1, 1, 1]) 创建一个一维向量
3. 使用列表推导式进行向量操作:
向量加法
addition_result = [x + y for x, y in zip(vector, [2, 2, 2])]
向量数乘
scalar_multiplication_result = [2 * x for x in vector]
向量点积
dot_product_result = sum([x * y for x, y in zip(vector, [2, 2, 2])])