# 对列表的每个元素应用函数（Apply a function to a list，map）

对列表中的每个元素应用一个函数。

## 怎么写

### map(f, list)

**f**

要应用的函数。接受一个函数。

**list**

要应用该函数的元素。接受一个列表。

## 示例

- $\displaystyle \operatorname{map}(x \mapsto x^{2}, \left[ 1, 2, 3 \right]) = \left[ 1, 4, 9 \right]$

  `map(x => x^2, [1, 2, 3])`

  在求解器中，`map(x => x^2, [1, 2, 3])` 的结果是 `[1, 4, 9]`。

  [在求解器中打开](https://zh.hilberon.com/?q=map%28x%20%3D%3E%20x%5E2%2C%20%5B1%2C%202%2C%203%5D%29)

## 相关函数

- [map_thread](https://zh.hilberon.com/library/map_thread)

  对多个列表中对应的元素应用一个函数。
- [list](https://zh.hilberon.com/library/list)

  由参数构成的列表。
- [most](https://zh.hilberon.com/library/most)

  去掉最后一个元素的列表。
- [length](https://zh.hilberon.com/library/length)

  列表中元素的个数。
- [nest](https://zh.hilberon.com/library/nest)

  对一个值反复应用函数。
- [last](https://zh.hilberon.com/library/last)

  列表的最后一个元素。

---
Source: https://zh.hilberon.com/library/map
