In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Introduce four python functions that are commonly used by great gods but not commonly used by you
Https://mp.weixin.qq.com/s/C2nw4JShmj1FlLeDy0IeRA
Today, we will talk about four functions: assert, map,filter,reduce.
Assert
Commonly known as assertion! That is to say, to assert a thing, if it is true, the program will continue; if it is false, an error will be reported.
How to use it?
Two uses
Assert
Assert
First kind
Def avg (marks):
Assert len (marks)! =
Return sum (marks) / len (marks)
Mark1 = []
Print ("Average of mark1:", avg (mark1))
The result is
AssertionError
The second kind
Def avg (marks):
Assert len (marks)! =, "List is empty."
Return sum (marks) / len (marks)
Mark2 = [55, 88, 78, 90, 79]
Print ("Average of mark2:", avg (mark2))
Mark1 = []
Print ("Average of mark1:", avg (mark1))
The result is
Average of mark2: 78.0
AssertionError: List is empty.
Map
In many cases, we perform the same operation on the data in a list, such as:
Items = [1,2,3,4,5]
Squared = []
For i in item s:
Squared. Append (iTunes * 2)
At this point, you can use map operation in the format:
Map (function_to_apply, list_input)
The specific operation is
Items = [1,2,3,4,5]
Squared = list (map (lambda x: Xerox * 2, items))
Of course, functions can be put in list.
Def multiply (x):
Return (xonomx)
Def add (x):
Return (xonomx)
Funcs = [multiply, add]
For i in range (5):
Value = list (map (lambda x: X (I), funcs))
Print (value)
# Output:
# [0, 0]
# [1, 2]
# [4, 4]
# [9, 6]
# [16, 8]
Of course, you can also perform str2id operations.
A = ['5','2','3','4','5']
Print (list (map (int, a)
# [5, 2, 3, 4, 5]
Filter
The filter function is to filter and filter a given condition.
Number_list = range (- 5,5)
Less_than_zero = list (filter (lambda x: X)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.