pec*_*eco 10 python arrays numpy
在熟悉 时numpy,我注意到numpy数组中有一个有趣的行为:
import numpy as np
arr = np.array([1, 2, 3])
scale = lambda x: x * 3
scale(arr) # Gives array([3, 6, 9])
Run Code Online (Sandbox Code Playgroud)
将此与普通 Python 列表进行对比:
arr = [1, 2, 3]
scale = lambda x: x * 3
scale(arr) # Gives [1, 2, 3, 1, 2, 3, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
我很好奇这怎么可能。numpy数组是否覆盖乘法运算符或其他什么?
这都是关于numpy 中的重写运算符
让我们重点关注每个函数的 lamda 函数;
1.numpy数组:
arr = numpy.array([1, 2, 3])
type(arr)
scale = lambda x: x * 3
scale(arr)
Run Code Online (Sandbox Code Playgroud)
这从数组中获取每个元素
2.正常列表:
a =[1,2,3]
type(a)
scale = lambda x: x * 3
scale(a)
Run Code Online (Sandbox Code Playgroud)
这将完整列表作为 x 并将此处的列表本身相乘
| 归档时间: |
|
| 查看次数: |
48517 次 |
| 最近记录: |