In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I would like to talk to you about what the 17 awesome operations in Python are, which many people may not know. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Life is short, I choose Python. So, have you really mastered Python?
1. Exchange variable
Sometimes, when we want to exchange the values of two variables, a general method is to create a temporary variable and then use it to exchange. For example:
Input
A = 5
B = 10
Create a temporary variable
Temp = a
A = b
B = temp
Print (a)
Print (b)
But in Python, we actually have a more concise way to write it:
two。 Convert all elements in list to a single string
Lists and strings are the most common data types in Python, and they encounter the need to convert each other. So, how do we convert all the elements in list to strings?
3. Find the element that appears most frequently in list
Given a list that contains multiple elements, what would you do if you looked for the elements that appeared the most? In the following figure, we introduce two methods, the first is to take advantage of the key parameter of the max () function, and the second is to use Counter.
1240
4. Determine whether two strings are anagram
The so-called anagram means that the letters and the number of letters used in two words are the same, but the positions of their letters are different, such as abc,bac,acb. In Python, Counter can solve this problem.
5. String reversal
To reverse the string is to convert 1234 to 4321. Here are three methods:
Method 1: the simplest slicing method, a [::-1] is equivalent to a [tail: head:-1].
Method 2: use Python with its own reversed () function, which can be used for list inversion.
Method 3: the third is to reverse integers by reversing data types and slices.
If you encounter a lot of questions and problems in the process of learning Python, you can add-q-u-n 227,435,450 which contains software video materials for free.
6. List reversal
Similar to string inversion, slicing can also be used for list inversion, such as the first method below. The second method calls the reversed () function mentioned above.
7. Two-dimensional matrix transpose
If you want to transpose the matrix, you can use the zip () function directly. Apart from that, there is no simpler way than import numpy.
8. Chain comparison
If you want to output an array of elements between 4 and 7, java says: if (b > 4 & & b < 7) {}, but Python is more human. Including the last sentence 1 = = b < 20, which is also very different from C and java.
9. Chain call
To implement a chained call in python, you only need to return the object itself in the function.
10. Copy list
If you are a C user who is just learning Python, you may want to write b = a, but this is wrong. In Python, a variable points to the label of an object. That is to say, in this way, b and a point to the same list in memory, and operating on b is equivalent to operating on a. Therefore, the correct writing methods are as follows:
Method 1: bachela [:].
Method 2: b=list (a).
Method 3: use the copy () function of Python 3 to copy list directly, similar to a [:].
Method 4: use copy.deepcopy ().
11. Determine whether a key is in the dictionary
If the target key is not in the dictionary, the following code returns None or default.
twelve。 Sort dictionaries by value
The built-in dictionary data type of Python is unordered, while key can be used to get the corresponding value. Sometimes we need to sort the output of the item in the dictionary according to value. The method is as follows:
Method 1: sort with the sorted function, where the key parameter is an lamda expression.
Method 2: sort with operator.itemgetter instead of lamda expression.
Method 3: if you only need to get the sorted key, you can use .get.
13. For... Else syntax
14. Separate list with commas
The following is the comma separation of the string list, numeric list, and mixed list, respectively.
1240
15. Merge dictionary
Method 1: Python 3.5 can have duplicate key values, print ({* * D1, * * D2}).
Method 2: create two lists in memory, and then create a third list. After the copy is complete, create a new dict and delete the first three lists.
Method 3: d1.update ().
16. Minimum and maximum indexes in list
17. Remove duplicates from list
Method 1: convert list to set, remove duplicates, and then go back to list.
Method 2: call OrderedDict in collections, which is similar to set.
After reading the above, do you have any further understanding of the 17 awesome operations in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.