[PY] [Lc]python Higher order function (anonymous/map/reduce/sorted)

Source: Internet
Author: User
anonymous functions
f = lambda x: x * xf(2) # x =2#4- 传入列表f = lambda x: x[2]print(f([1, 2, 3])) # x = [1,2,3]
Map using

Incoming function body

def f(x):    return x*xr = map(f, [1, 2, 3, 4]) #函数作用在可迭代对象的每一项#[1, 4, 9, 16]- 另一个例子list(map([1, 2, 3, 4, 5, 6, 7, 8, 9], lambda x: x * x))
Reduce usage
from functools import reducedef add(x, y):    return x*10 + yprint(reduce(add, [1, 2, 3, 4])) #函数作用在可迭代对象的每一项聚合# 1234
Sorted inquiry

Reference:
Higher order function Map/reduce

Sorted, the official document is very good, can learn a lot of things

sorted(iterable, key=None, rev

[PY] [Lc]python Higher order function (anonymous/map/reduce/sorted)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.