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 > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about the practical but uncommon tips in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
"Talk is cheap show me the code", a programmer's note, says that half a day is not as convincing as a few lines of code. Next, we'll use code to illustrate tips that are not very common but very useful.
Variable value exchange
This is a very Python (Pythonic) value interaction method, leaving out the intermediate variables in C and C++.
Concatenate all the elements of list into a string
This technique actually uses the join () method of a string, the connector can be any string, and the join can be any iterable object (iterables), such as lists, dictionaries, collections, tuples, etc.
Find out the elements that appear most frequently in list
Here are two methods. If you are interested, you can test which method is faster.
Determine whether two strings contain the same character
In fact, it is judged that the characters in the two strings are the same and the number of occurrences of each character is the same.
Reverse string
There are three ways, the first is the most common, and the third is to reverse integers by reversing strings.
Reverse the list
The method of reversing the list here is actually the same as the method of reversing the string above.
Transpose 2D array
The built-in function zip,zip is very powerful here, so you can learn it specially.
Chain comparison
This is the number comparison in mathematics, which is easier than C and C++.
Chained function call
(product if b else add) this expression returns the function object, the call to the function object
List replication
Shallow copy and deep copy of the list. Note that different scenarios require different copies when using them.
Get method of Dictionary
D.get ('centering, 3) takes key as' c'. If the key does not exist, it returns 3 by default.
Sort dictionaries by value
Three methods are given, all of which are very good. The first one uses the lambda function.
This can also be used: for else
This usage is rare and depends on the situation.
Symbolize a list into a string
This is about the same as before, using map to convert integers into strings
Merge dictionary
It is usually the update method, the first two are rare
Find the index of the maximum or minimum value in the list
This method is also rare, especially with proprietary methods such as _ _ getitem__
Remove duplicate elements from the list
Convert list to set and then back to list, which is a classic list deduplication method
These are the practical but uncommon tips in Python shared by the editor. 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.