Get the App
SLTechnology News&Howtos  ›  Development  › 

Share the correct usage of several commonly used functions in python

Shulou Source: shulou.com Published: 2022-06-02 17:30:22 09月13日 Update

This article mainly explains the correct use of several commonly used functions in sharing python. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the correct usage of several common functions in sharing python.

1 lambda

Anonymous function, the usage is as follows:

# lambada parameters, parameters: returned expression

Example 1:

F = lambda x, y: X * yprint (f (2,3)) # 6

Example 2:

R = (lambda x, y: X filter) (1,2) print (r) # 32 filter

Filter (function, sequence): execute function (item) on the item in sequence in turn, and return the item whose execution result is True into a filter object (iterable) (depending on the type of sequence).

Example:

A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 531509025 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Def gt_5 (x): return x > 5 r = filter (gt_5, range (10)) print (list (r)) # [6,7,8,9] 3 map

Map (function, sequence): execute function (item) on the item in sequence in turn. See the execution result to form a map object (which can be iterated).

Example:

Def mysum (x, y): return x + y r = map (mysum, range (5), range (5,10) print (list (r)) # [5,7,9,11,13] 4 reduce

In python3, reduce has been removed from the global namespace and needs to be imported from functiontools.

Reduce (function, sequence, starting_value): iterates over the item sequence in sequence to call function, and if there is a starting_value, it can be used as an initial value.

Example:

A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 531509025 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Def mysum (x, y): return x + y from functools import reducer = reduce (mysum, range (10)) print (r) # 455

Example: calculation 1! + 2! +. + 10!

Def factorial (n): if n = = 1: return 1 return n*factorial (nMel 1) r = reduce (lambda x, y: X + y, map (factorial, range (1,11)) print (r) # 4037913 Thank you for reading. This is the content of "sharing the correct usage of several commonly used functions in python". After the study of this article, I believe you have a deeper understanding of the problem of sharing the correct usage of several commonly used functions in python, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Learning examples functions commonly used parameters problems iterations good like-minded help each other help each other content learning exchanges learning tutorials objects kids buddies tutorials e-books Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi vpn Huawei OPPO Reno