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

Introduction to python built-in function syntax

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

This article introduces the relevant knowledge of "introduction to the grammar of python built-in functions". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A preface

When writing Python programs or tool scripts, you need to complete a function, you can choose to write a specific function to achieve the goal, of course, you can also do it through the anonymous / Python built-in function. This question describes the common Python anonymous, built-in functions-lambda,map,filter,reduce.

Two examples

2.1 Lambda is an anonymous function

Its syntax is: lambda parameters:express

Parameters: optional, usually comma-separated form of variable expressions, namely positional parameters.

Expression: cannot contain branches or loops (but conditional expressions are allowed) or return (or yield) functions. If it is a tuple, it is enclosed in parentheses.

Call the lambda function, and the result returned is the result of evaluating the expression.

Returns yes or no based on whether the parameter is a multiple of 2

In [21]: s = lambda x: "yes" if x% 2 = 0 else "no"

In [22]: s (3)

Out [22]: 'no'

In [24]: s (4)

Out [24]: 'yes'

In the above example, the if...else statement is reduced to a single conditional expression with the syntax of:

Expression1 if An else expression2

If An is True, the result of the conditional expression is expression1, otherwise it is expression2.

The advantage of using anonymous functions is that the code is concise, but it is not easy to read. It is recommended that for.. in... if syntax traverse iterations.

2.2 filter filtering

Its syntax is filter (function, sequence).

The meaning is that function (item) is called iteratively on the item in sequence, and the element is retained or discarded depending on whether the execution result is True or False. And compose the retained item into a List/String/Tuple (depending on the type of sequence)

Filter () acts the passed function on each element in turn, and then decides whether to retain or discard the element based on whether the return value is True or False.

In [15]: filter (lambda x:True if x% 3 = = 0 else False, range (10))

Out [15]: [0,3,6,9] # returns list

In [17]: filter (lambda x:True if x% 3 = = 0 else False, (1 else False 2, 3 5, 6 7, 8, 9))

Out [17]: (3,6,9) # returns Tuple tuple

2.3 map Mappin

Its syntax is map (function, sequence):

The use of map () is similar to filter (), which also puts the sequence into the function for operation, but regardless of the result, map () returns the result, that is, the number of result elements is as many as sequence, and filter returns a qualified value. This is the main difference between map () and filter (). Note that function in both filter () and map () must have a return value. Also get the value that can be divisible by 3, map returns a list of True/False, and filter returns a result set of elements that match the criteria.

In [18]: list (map (lambda x:True if x% 3 = = 0 else False, range (10))

Out [18]: [True, False, False, True]

2.4 reduce

Its syntax is reduce (function, sequence, starting_value).

The meaning is that function is called on the item in sequence in turn. Reduce (f, [x1, x2, x3, x4]) = f (f (x1, x2), x3), x4)

If there is a starting_value, it can also be called as an initial value, such as the list summation of 1-100:

In [11]: def add (xmemy):

...: return x + y

...:

In [12]: reduce (add, range (1,100))

Out [12]: 4950

In [13]: reduce (add, range (1,10))

Out [13]: 45

# with the starting_value parameter, which is equivalent to 1 "2" 3 "4" 5 "6" 7 "8" 9 "10" 20

In [14]: reduce (add, range (1,10), 20)

Out [14]: 65

This is the end of the introduction to the syntax of python built-in functions. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Network Security

Wechat

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

12
Report