In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the high-level functions commonly used in Python. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Lambda
They are also called anonymous functions in other languages. If you don't want to use a function twice in your program, you might want to use lambda expressions, which are exactly the same as normal functions.
Lambda argument: manipulate (argument)
Lambda parameters: operations (parameters)
Add = lambda x, y: X + yprint (add (3,5)) # Output: 8a = [(1,2), (4,1), (9,10), (13,-3)] def f (x): return x [1] # a.sort (key=f) a.sort (key=lambda x: X [1]) print (a) # Output: [(13,-3), (4, 1), (1, 2), (9, 10)] sortedsorted (iterable, *, key=None) Reverse=False)
Returns a new sorted list from item in iterable.
There are two optional parameters that must be specified as keyword parameters.
Key specifies a function with one argument to extract the comparison key from each list element: key=str.lower. The default value is None (direct comparison elements).
Reverse is a Boolean value. If set to True, the list elements are sorted in the order in which each comparison is reversed.
The sorting of the built-in sorted () function is stable. The sorting is stable if you ensure that the relative order of elements that are more equal is not changed.
Ternary expression
Ternary operators are usually called conditional expressions in Python, and these expressions are based on the conditional judgment of true / false.
It allows quick judgment with a simple line, rather than using complex multi-line if statements. This is very useful most of the time and can make the code simple and maintainable.
# if the condition is true, return true otherwise return false condition_is_true if condition else condition_is_falseif condition: result = condition_is_trueelse: result = condition_is_falsemap
Map (function, iterable,...)
Returns an iterator that applies function to each iterable item, resulting in a result. If additional iterable parameters are passed, function must take multiple parameters and apply them to projects in all iterations in parallel. When multiple iterators are used, the iterator stops when the shortest iterator is exhausted.
In [54]: list1 = [1, 2, 3, 4, 5, 6] In [55]: list2 = [4, 3, 7, 1, 9] In [56]: list (map (str, list1)) Out [56]: ['1','2','3','4','5','6'] In [57]: list (lambda x, y: x, list1, list2) Out [57]: [5, 5, 10, 5, 14] enumerate
Enumerate (iterable, start=0)
Returns an enumerated object. The iterable must be a sequence, an iterator, or other object that supports iteration. The _ _ next__ () method of the iterator returned by enumerate () returns a tuple containing a count (starting with start, with a default of 0) and the value obtained by iterating through the iteration.
> seasons = ['Spring',' Summer', 'Fall',' Winter'] > list (enumerate (seasons)) [(0, 'Spring'), (1,' Summer'), (2, 'Fall'), (3,' Winter')] > list (enumerate (seasons, start=1)) [(1, 'Spring'), (2,' Summer'), (3, 'Fall'), (4,' Winter')] zip
Zip (* iterables)
Make an iterator to aggregate the elements from each iterator.
Returns the iterator of the tuple, where the I tuple contains the I th element from each parameter sequence or iteration. The iterator stops when the shortest input iteration is exhausted. Using a single iteration parameter, it returns a 1-tuple iterator. With no arguments, it returns an empty iterator.
Using zip () with the * operator can be used to extract the list:
> x = [1,2,3] > > y = [4,5,6] > > zipped = zip (x, y) > list (zipped) [(1,4), (2,5), (3,6)] > > x2, y2 = zip (* zip (x, y)) > x = list (x2) and y = list (y2) Truedata = zip (list1, list2) data = sorted (data) list1, list2 = map (lambda t: list (t), zip (* data)) filter
Filter (function, iterable)
Construct an iterator with those iterable elements that function returns true. An iterable can be a sequence, a container that supports iterations, or an iterator. If function is None, it is assumed that the identity function is false, that is, all elements of false are deleted.
# filter the even numbers between 0 and 10 In [8]: list (filter (lambda x: x% 2) range (10)) Out [8]: [0,2,4,6,8] reduce
The use of the reduce function is very similar to map, which is also a function f and a list, but the entry parameters of the function must be two. Reduce also calls each element repeatedly and returns the final value, while map returns a list.
These are the high-level functions commonly used in Python that Xiaobian shares with you. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.