In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the important functions of Python". In daily operation, I believe many people have doubts about the important functions of Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the important functions of Python?" Next, please follow the editor to study!
1. Generator-memory efficiency
A generator is used when you intend to calculate a large number of results, but at the same time you want to avoid allocating memory for all results at the same time. In other words, they generate values immediately, but do not store previously generated values in memory, so you only need to iterate them once. Generators are often used to read large files or to generate infinite sequences using the keyword yield. In most data science projects, the author finds it useful.
Def gen (n): # an infinite sequence generator that generates integers > = n while True: yield n n + = 1G = gen (3) # starts at 3 print (next (G)) # 3 print (next (G)) # 4 print (next (G)) # 5 print (next (G)) # 6
two。 Virtual environment-isolation
If you finish reading this article, you can only remember one of them, which should be the use of a virtual environment.
Python applications often use many different software packages developed by developers with complex dependencies. Different applications are developed by a specific library setting, and the results cannot be copied by other versions of the library. No installer can meet the requirements of all applications.
Conda create-n venv pippython=3.7 # select python version source activate venv... Source deactivate
Therefore, it is critical to create a separate virtual environment venv for each application, which can be done with pip or conda.
3. List derivation-Compact Code
Many people think that lambda, map and filter are functions that every beginner should learn. Although I think these functions are worth paying attention to, they are not particularly useful most of the time because of their lack of flexibility. Lambda is a way to generate a single-use function into a line. If the function is called multiple times, the performance will degrade. Map, on the other hand, applies a function to all elements in the list, while filter gets a subset of elements in the collection that meet user-defined criteria.
Add_func = lambda z: Z * * 2 is_odd = lambda z: Z% 2 = = 1 multiply = lambda xrecoery y: Xyogy aList = list (range (10)) print (aList) 4 # [0,1,2,3,4,5,6,7,8,9]
List derivation is a simple and flexible way to create new lists from other lists with flexible expressions and conditions. It consists of a square bracket with an expression or function that can be applied to each element in the list only if the element meets certain conditions. It can also handle nested lists through nesting, which is much more flexible than using map and filter.
# Syntax of list comprehension [_ expression (x) for x in aList if optional_condition (x)] print (list (map (add_func, aList)) print ([x * * 2 for x in aList]) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] print (filter (is_odd) AList)) print ([x for x in aList if x% 2 = = 1]) # [1,3,5,7,9] # [1,3,5,7,9]
4. List Operation-Loop list
Python allows negative indexes in aList [- 1] = = aList [len (aList)-1]. Therefore, you can get the penultimate element in the list by calling other elements such as aList [- 2].
You can also slice the list with the aList [start:end:step] syntax, which includes the starting element but not the ending element. Therefore, when you call aList [2:5], you get [2,3,4]. You can also reverse the list by calling aList [::-1], which I find very concise.
Lists can also be extracted into separate elements, or into elements and sublists, which are mixed and marked with an asterisk.
A, b, c, d = aList [0:4] print (f = {a}, b = {b}, c = {c}, d = {d}') # a = 0, b = 1, c = 2, d = 3a, * b, c, d = aList print (f = {a}, b = {b}, c = {c}, d = {d}') # a = 0, b = [1,2,3,4,5,6,7], c = 8 D = 9
5. Compression (Zipping) and enumeration (enumerate)-- for loop
The Zip function creates an iterative program that aggregates elements from multiple lists. It allows parallel traversal of lists and parallel sorting in for loops. You can decompress it with an asterisk.
NumList = [0,1,2] engList = ['zero',' one', 'two'] espList = [' cero', 'uno',' dos'] print (list (zip (numList, engList, espList) # [(0, 'zero',' cero'), (1, 'one',' uno'), (2, 'two',' dos')] for num, eng, esp in zip (numList, engList) EspList): print (f'{num} is {eng} in English and {esp} in Spanish.') # 0 is zero in English and cero in Spanish. # 1 is one in English and uno in Spanish. # 2 is two in English and dos in Spanish.Eng = list (zip (engList, espList, numList)) Eng.sort () # sort by engList a, b, c = zip (* Eng) print (a) print (b) print (c) # ('one',' two', 'zero') # (' uno', 'dos',' cero') # (1,2,0)
At first glance, Enumerate may seem scary, but you'll find it convenient to use it in many cases. It is an automatic counter that is often used in for loops, so there is no need to create and initialize variable counters with counter = 0 and counter + = 1. Enumerate and zip are two powerful tools used to create for loops.
UpperCase = ['A', 'lower,' C','D','E','F'] lowerCase = ['A','b','c','d','e','f'] for I, (upper, lower) in enumerate (zip (upperCase, lowerCase) 1): print (f'{I}: {upper} and {lower}.) # 1: An and a. # 2: B and b. # 3: C and c. # 4: D and d. # 5: E and e. # 6: F and f. At this point, the study of "what are the important functions of Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.