Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Share the correct usage of several commonly used functions in python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report