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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this "Python example code analysis" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Python example code analysis" article.
1. Exchange two variables # a = 1; b = 2a, b = b, a # print (AMagneb) > > 2 1
Let's start with a classic: simply swapping assignment positions to swap the values of variables-in my opinion, this is the most intuitive way. You do not need to use temporary variables. It even applies to more than two variables.
two。 Multiple variables are assigned to a print b, * c = [1 print 2, 3, 4, 5] # and > 1 2 [3, 4, 5]
Swapping variables is actually a special case where python can allocate more than one variable at a time. Here, you can use it to assign list elements to a given variable, which is also known as untabling. * the remaining values will be packaged again, which will result in a sublist of c. It can even be used in other locations of * (such as the beginning or middle of the list).
3. Sum every two elements of the list # a = [1 print, 2, 3, 4, 5, 6] s = sum (a [1:: 2]) # sum (s) > > 12
There is no need for a special reduce function here, sum just adds items for each given iteration. The extended slice syntax [:] is used here to return the second element. You can read it as [start: stop: step], so [1DV] translates from the element of index 1 (the second element) to the end of the list (the second parameter does not give an argument), and always takes two steps.
(recommended tutorial: python tutorial)
4. Delete multiple elements of the list # a = [1, del 2, 3, 4] del a [:: 2] # print (a) > > [2,4]
Extended slicing syntax can also be used to delete multiple list elements at a time.
5. Read the file into the line array c = [line.strip () for line in open ('file.txt')] # print (c) > > [' test1', 'test2',' test3', 'test4']
Using the python inline for loop, you can easily read files into an array of lines. You need to use strip () to remove the subsequent disconnects. If you want to keep them or if they are not important to you, you can use a shorter line:
C = list (open ('file.txt')) # print (c) > > [' test1\ n', 'test2\ n', 'test3\ n', 'test4\ n']
It's really easy to read files in Python. Note: you can also use the readlines () method if you prefer.
6. Write the string to the file with open ('file.txt',' a') as f: f.write ('hello world') # print (list (open (' file.txt') > > ['test1\ n', 'test2\ n', 'test3\ n', 'test4\ n', 'hello world']
With the help of the With statement, you can write the content directly to the file. Be sure to open the file in the correct mode (where "a" means additional content).
7. Create the list l = [('Hi'+ x) for x in ['Alice',' Bob', 'Pete']] # print (l) > > [' Hi Alice','Hi Bob','Hi Pete']
You can use inline for loops to dynamically create lists from other lists. You can modify the value directly, just like the string concatenation in this example.
8. List mapping l = list (map (int, ['1percent,' 2percent,'3']) # print (l) > > [1,2,3]
You can also use the Pythons map () function to cast each list element to another type.
(recommended micro-course: python3 basic micro-course)
9. Collection creation squares = {xanthates 2 for x in range (6) if x
< 4 }# print(squares) >> {0,1,4,9}
The collection is the same. In addition to inline for loops, you can even add conditions directly!
10. Palindrome check # phrase = 'deleveled'isPalindrome = phrase = = phrase [::-1] # print (isPalindrome) > > true
Palindromes are a series of characters that read the same characters forward and backward. If a given string is a palindrome, you usually need some loops and conditions to check. In Python, you only need to compare the string with its reverse string. In addition to using the slicing operator [::-1], you can also use the reverse () function to reverse strings.
The above is about the content of this article on "Python example Code Analysis". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to 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.