美文网首页
python函数学习

python函数学习

作者: carebon | 来源:发表于2021-10-03 21:01 被阅读0次

1.lambda匿名函数

lambda x: vocab.stoi[x]

x是参数,:后面的是返回值

2.map函数

map() 会根据提供的函数对指定序列做映射。

第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

语法
map() 函数语法:

map(function, iterable, ...)

python 3实例

>>> def square(x) :         # 计算平方数
...     return x ** 2
... 
>>> map(square, [1,2,3,4,5])    # 计算列表各个元素的平方
<map object at 0x100d3d550>     # 返回迭代器
>>> list(map(square, [1,2,3,4,5]))   # 使用 list() 转换为列表
[1, 4, 9, 16, 25]
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))   # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
>>> 

相关文章

网友评论

      本文标题:python函数学习

      本文链接:https://www.haomeiwen.com/subject/acebnltx.html