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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。