In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you what are the advanced features of Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.
There is no need to say more about Python. Just take a look at your own language. But have you get all the advanced features hidden by Python? In this article, the author lists five slightly advanced features in Python and how to use them. Take a look!
Python is a beautiful language, easy to use but very powerful. But can you really use all the functions of Python?
The advanced features of any programming language are usually discovered through a great deal of experience. Let's say you're writing a complex project and looking for an answer to a question on stackoverflow. Then you suddenly find a very elegant solution that uses Python features you never knew!
This way of learning is so interesting: through exploration, you stumble upon something.
Here are five advanced features of Python and their usage.
Lambda function
The Lambda function is a relatively small anonymous function-- anonymous means that it doesn't actually have a function name.
The Python function is usually defined using the def a_function_name () style, but for the lambda function, we didn't name it at all. This is because the function of the lambda function is to perform some simple expression or operation without having to fully define the function.
The lambda function can take any number of arguments, but there can only be one expression.
X = lambda a, b: a * b print (x (5,6)) # prints' 30' x = lambda a: a print 3 + 3 print (x (3)) # prints' 12'
Look how simple it is! We performed some simple mathematical operations without defining the entire function. This is one of the many features of Python that make it a clean, simple programming language.
Map function
Map () is a built-in Python function that applies functions to elements in various data structures, such as lists or dictionaries. This is a very clean and readable way to perform this kind of operation.
Def square_it_func (a): return a * a x = map (square_it_func, [1,4,7]) print (x) # prints'[1,16,47] 'def multiplier_func (a, b): return a * b x = map (multiplier_func, [1,4,7], [2,5,8]) print (x) # prints' [2,20,56] 'look at the above example! We can apply functions to single or multiple lists. In fact, you can use any Python function as input to the map function, as long as it is compatible with the sequence element you are working on.
Filter function
Filter built-in functions are very similar to map functions in that they also apply functions to sequence structures (lists, tuples, dictionaries). The key difference between the two is that filter () will return only the elements that the application function returns True.
For more information, please see the following example:
# Our numbers numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] # Function that filters out all numbers which are odd def filter_odd_numbers (num): if num% 2 = 0: return True else: return False filtered_numbers = filter (filter_odd_numbers, numbers) print (filtered_numbers) # filtered_numbers = [2,4,6,8,10,12,14]
We not only evaluated the True or False,filter () function of each list element, but also ensured that only elements matching True were returned. It is very easy to handle the two steps of checking expressions and building return lists.
Itertools module
Python's Itertools module is a collection of tools that deal with iterators. An iterator is a data type that can be used in for loop statements, including lists, tuples, and dictionaries.
Using the functions in the Itertools module allows you to perform many iterator operations, which usually require multi-line functions and complex list understanding. For the magic of Itertools, look at the following example:
From itertools import * # Easy joining of two lists into a list of tuples for i in izip ([1,2,3], ['The count,' baked,'c']): print I # ('ABA, 1) # (' baked, 2) # ('The count () function returns an interator that # produces consecutive integers, forever. This # one is great for adding indices next to your list # elements for readability and convenience for i in izip (count (1), [Bob', 'Emily',' Joe']): print I # (1, 'Bob') # (2,' Emily') # (3, 'Joe') # The dropwhile () function returns an iterator that returns # all the elements of the input which come after a certain # condition becomes false for the first time. Def check_for_drop (x): print 'Checking:', x return (x > 5) for i in dropwhile (should_drop, [2,4,6,8,10,12]): print 'Result:', I # Checking: 2 # Checking: 4 # Result: 6 # Result: 8 # Result: 10 # Result: 12 # The groupby () function is great for retrieving bunches # of iterator elements which are the same or have similar # properties a = sorted ([1,2,1,3,2,1) 2, 3, 4, 5]) for key, value in groupby (a): print (key, value), end='') # (1, [1,1]) # (2, [2,2]) # (3, [3]) # (4, [4]) # (5, [5])
Generator function
The Generator function is an iterator-like function, that is, it can also be used in for loop statements. This greatly simplifies your code and saves a lot of memory compared to a simple for loop.
For example, we want to add all the numbers from 1 to 1000. The * * section of the following code block shows you how to use the for loop to do this calculation.
If the list is small, such as 1000 rows, the memory required for calculation is OK. But if the list is very long, such as a billion floating points, this can be a problem. With this for loop, there will be a large number of lists in memory, but not everyone has a * RAM to store so many things. The range () function in Python does the same, building lists in memory.
The second part of the code shows the use of the Python generator function to sum a list of numbers. The generator function creates elements and stores them in memory only if necessary, one at a time. This means that if you want to create billions of floating point numbers, you can only store them in memory one at a time! the xrange () function in Python 2.x uses generator to build lists.
The above example shows that if you want to generate a list for a large range, you need to use the generator function. This method is especially important if you have limited memory, such as using a mobile device or edge computing.
That is, if you want to iterate over the list multiple times and it is small enough to fit in memory, then * use the for loop or the range function in Python 2.x. Because the generator and xrange functions will generate new list values every time you visit them, while the Python 2.x range function is a static list, and integers have been placed in memory for quick access.
# (1) Using a for loopv numbers = list () for i in range (1000): numbers.append (iTun1) total = sum (numbers) # (2) Using a generator def generate_numbers (n): num, numbers = 1 [] while num < n: numbers.append (num) num + = 1 return numbers total = sum (generate_numbers (1000)) # (3) range () vs xrange () total = sum (range (1000 + 1)) total = sum (xrange (1000 + 1)) what are the advanced features of Python? I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.