1. 遍历
def square(x) : # 计算平方数 return x ** 2 a = [1,2,3,4,5] for i in range(len(a)): a[i] = square(a[i])
2. 推导式
def square(x) : # 计算平方数 return x ** 2 a = [1,2,3,4,5] a = [square(item) for item in a]
3. map函数
def square(x) : # 计算平方数 return x ** 2 a = [1,2,3,4,5] a = list(map(square, a))
一维数组变二维数组,行变列:
a = pd.Series([1,2,3,4,5]) print(a) b = a.values[:, None] # 一维变二维 行变列 print(b) c = b.T # 转置 print(c)