大型数组运算
一、 解决问题
在大数据集(比如数组或网格)上面执行计算
二、解决方案
NumPy 库 提供了一个数组对象
三、代码说明
#!/usr/bin/env python
x = [1, 2, 3, 4]
y = [5, 6, 7, 8]
print(x * 2)
#print(x + 10) 报错
print (x + y)
import numpy as np
ax = np.array(x)
ay = np.array(y)
print(ax * 2) #->[2 4 6 8] 2倍
print(ax + 10) #->[11 12 13 14]都+10
print(ax + ay) #->[ 6 8 10 12]两个相加
print(ax * ay) #->[ 5 12 21 32]两个相乘
四、关联知识
五、总结
- NumPy 是Python领域中很多科学与工程库的基础,同时也是被广泛使用的最大最复杂的模块
- 通常我们导入 NumPy 模块的时候会使用语句 import numpy as np 。 这样的话你就不用再你的程序里面一遍遍的敲入 numpy ,只需要输入 np 就行了,节省了不少时间。
六、代码地址
github地址:https://github.com/weichen666/python_cookbook
目录/文件:eight_selection/learn_data_date_time_numpy.py