Python 的 map(~)
方法返回一个迭代器,该迭代器将指定的函数应用于可迭代的每个项目。
参数
1. function
| function
应用于可迭代的每个元素的函数。
2. iterable
| iterable
我们将对其元素应用函数的可迭代对象。可以指定多个迭代,但是,当提供的最短的迭代耗尽时,迭代器将停止。
返回值
一个迭代器,将提供的函数应用于可迭代对象中的每个元素。
例子
基本用法
要将列表中的每个字母大写:
letters = ['a', 'b', 'c']
capitalized_letters = map(str.upper, letters)
print(list(capitalized_letters))
['A', 'B', 'C']
拉姆达函数
返回每个数字的立方:
nums = [1, 2, 3, 4, 5]
cube_nums = map(lambda x: x**3, nums)
print(list(cube_nums))
[1, 8, 27, 64, 125]
相关用法
- Python map()用法及代码示例
- Python matplotlib.patheffects.withTickedStroke用法及代码示例
- Python matplotlib.axes.Axes.step用法及代码示例
- Python matplotlib.texmanager.TexManager.get_rgba用法及代码示例
- Python matplotlib.backend_bases.MouseEvent用法及代码示例
- Python matplotlib.collections.RegularPolyCollection.set_hatch用法及代码示例
- Python numpy ma.MaskedArray.view用法及代码示例
- Python matplotlib.patches.Rectangle用法及代码示例
- Python matplotlib._api.deprecation.deprecated用法及代码示例
- Python matplotlib._api.select_matching_signature用法及代码示例
- Python matplotlib.figure.Figure.align_xlabels用法及代码示例
- Python matplotlib.pyplot.step()用法及代码示例
- Python matplotlib.figure.SubFigure.add_subplot用法及代码示例
- Python matplotlib.collections.BrokenBarHCollection.set_hatch用法及代码示例
- Python math.cos()用法及代码示例
- Python math.cosh()用法及代码示例
- Python matplotlib.collections.PolyCollection.sticky_edges用法及代码示例
- Python matplotlib.axes.Axes.barbs用法及代码示例
- Python matplotlib.textpath.TextToPath.get_text_path用法及代码示例
- Python math.acosh()用法及代码示例
- Python main()用法及代码示例
- Python matplotlib.figure.Figure.add_axes用法及代码示例
- Python matplotlib.axes.Axes.pie()用法及代码示例
- Python matplotlib.collections.TriMesh.set_hatch用法及代码示例
- Python matplotlib.collections.StarPolygonCollection.sticky_edges用法及代码示例
注:本文由纯净天空筛选整理自Arthur Yanagisawa大神的英文原创作品 Python | map method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。