Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

17 commonly used Operations sharing in Python

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "17 commonly used operations sharing in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "17 common operations sharing in 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.

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.

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.

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.

Thank you for reading, the above is the content of "17 commonly used operations sharing in Python". After the study of this article, I believe you have a deeper understanding of the 17 operations commonly used in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report