In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the skills of writing Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the skills of writing Python"?
1. Import module
Do you often have a headache about entering a long list of module indexes when calling a module? To be honest, it may be bearable when the number is small, and once the scale of the program goes up, it is also a project to be reckoned with.
Import urllib.request url = r 'http://www.landsblog.com' req = urllib.request.Request (url) response = urllib.request.urlopen (req) form urllib import request url = r' http://www.landsblog.com' req = request.Request (url) response = request.urlopen (req)
Does this save a little time?
But how to solve the problem that such abbreviations may cause duplicate module names?
From module_a import fun as a_fun from module_b import fun as b_fun
This method is also suitable for modules with long module names. What impressed me most is the BeautifulSoup module.
From bs4 import BeautifulSoup as BS html =''. '' Soup = BS (html)
Save time and effort.
two。 About "_" # #
This is a very useful feature, but few people know about it.
When you type the code in the interface to get a temporary result, but do not save it with a variable name, you can use "_" to get the most recent temporary result.
> 1 + 1 2 > 2
Stores the last output value in "_". This is very useful in interactive mode when you do not save the results of the calculation during the process, or you want to see the output of the last step.
3. Merge strin
This is a clich é. When we need to merge several strings, we habitually use "+" as a means of concatenating strings.
However, because immutable objects cannot be modified after they are generated in memory, the merged string will re-open up a piece of memory space for storage. This is like a snowball, running out of memory quickly.
# Bad string = [def fun (string): all_string =''for i in string: all_string + = I return all_string # Good string = [' ahem] for i in string: all_string + = I return all_string # Good string = ['axiang] for i in string: all_string + = I return all_string # Good string = [' ahem: all_string + = I return all_string # Good string] 'h'] def fun (string): all_string = '.join (string) return all_string
4. Powerful zip ()
It is a built-in function of Python, and the zip function takes any number of sequences (including 0 and 1) as arguments and returns a list containing tuple. The zip () function can simplify your code in many scenarios.
# # column-column interchange of matrices # Bad a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] re_a = [[Rowan [col] for row in a] for col in range (len (a)] > > [1, 4, 7], [2, 5, 8], [3, 6, 9] # Good a = [1, 2, 3], [4, 5, 6], [7, 8] 9]] re_a = list (zip (* a)) > [[1, 4, 7], [2, 5, 8], [3, 6, 9]] # # Exchange the bond value of dict a = {'aquidus: 1,' baked: 2, 'cased: 3,' dumped: 4} def reverse_dict (a): new_dict = {} for k V in m.items (): new_ neighbors [v] = k return new_dict a = {'aqu: 1,' baked: 2, 'cations: 3,' dudes: 4} def reverse_dict (a): K = a.keys () v = a.values () new_dict = dict (zip (v, k)) return new_dict # # merge list adjacent terms a = [1,2,3,4 5, 6] list (zip (a [:: 2], a [1lav vig 2]) > [(1,2), (3,4), (5,6)]
5. Variable value exchange
Tmp = an a = bb = tmp a, bb = b, a
6. Get the index (array subscript) in the loop?
A = [8,23,45,12,78] for index, value in enumerate (a): print (index, value)
7. How do I catch multiple exceptions on only one line?
Try: pass except (ExceptionA,ExceptionB,.) As e: pass
8. Split the list into blocks of the same size?
A = [1,2,3,4,5,6] list (zip (* [iter (a)] * 2)) > [(1,2), (3,4), (5,6)]
9. How do I find the subscript of an element in the list?
A = ['await,' baked, 'caged,' dashed, 'eBay,' f'] aa_i = a.index (a) > 0
10. How to quickly reverse a string?
A = 'Python is a powerful languange.' Listlist_a = list (a) list_a.reverse () re_a = '.join (list_a) a =' Python is a powerful languange.' Re_a = a [::-1]
11. Numerical comparison
X = 2 if 1
< x >> 2 if 1
< x >0: print (x) > 2
twelve。 Open the file gracefully
Usually, when using a stream object similar to a file, call the close method to close it after you finish using it. With... The as statement provides a very convenient alternative: when open opens the file, it assigns the returned file stream object to f and then uses it in the with statement block. After the block of with statements is completed, the file is automatically closed secretly.
With open ('nothing.txt','r') as f: f.read ()
13. Say goodbye to your memory
Crash = dict (zip (range (10 * * 0xA), range (10 * * 0xA) so far, I believe you have a deeper understanding of "what are the skills of writing Python". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.