Home >Backend Development >Python Tutorial >Three time-saving Python tips!

Three time-saving Python tips!

王林
王林forward
2023-04-12 14:25:181417browse

Three time-saving Python tips!

My recent work content: automatic file moving, image processing and data cleaning. I summarized some Python tips during the programming process to make my code neat and easy to understand. Next, I will share with you 3 time-saving Python tips.

Three time-saving Python tips!

Reverse the list

I recently had a project where I needed to reverse the list, which was initially done through slicing. But in fact, you can also reverse a list in python using the built-in function reverse() method.

Start by creating a list.

# 创建一个列表
mylist = list(np.arange(0,100))

The two methods of reversing the list are:

# 使用切片反转列表
newlist = mylist[::-1]

# 使用内置的 reverse() 反转列表
mylist.reverse()

Both methods can reverse the list, but it should be noted that the built-in function reverse() will change the original list and the slicing method is to create a new list.

Let’s compare the execution time ⏳

Three time-saving Python tips!

reverse() and the execution time of list slicing

Obviously, the built-in function reverse() 3 times faster than list slicing method!

In my work project, in order to realize the automatic movement of system files, I created a list of location values. The early Python program was written using list slicing. Now I have rewritten the code to use the reverse() function to make the program execute faster.

Okay, let’s introduce the second technique.

Swapping two values ​​with one line of code

Swapping two variable values ​​with one line of code is a truly Pythonic way.

Unlike other programming languages, Python does not need to use temporary variables to exchange two numbers or values. Take a simple example:

# 创建两个变量
variable_1 = 100 
variable_2 = 500

To swap the values ​​of variable_1 and variable_2, you only need to use one line of code.

变量_2,变量_1 = 变量_1,变量_2

Use one line of code to exchange two values, which is simplified into the following form.

Three time-saving Python tips!

Of course, my actual work project is a little more complicated than this, using a dictionary with a list as a value for each key.

md[key_2],md[key_1] = md[key_1],md[key_2]

Through the above techniques, my work saves multiple iterations and complex data conversion, and reduces the execution time.

Using for loops within functions

We all like to create custom functions to perform our own specific tasks. Then use a for loop to iterate these functions and repeat the task multiple times.

However, using a function within a for loop takes longer to execute because the function is called on each iteration.

In contrast, if the for loop is implemented inside a function, the function will only be called once.

In order to explain more clearly, let’s give an example!

First create a simple string list:

listofstrings = ['苹果','橙子','香蕉','菠萝','葡萄']

Create two functions, with for loops inside and outside the functions, starting with the simple one.

# 在函数内部创建一个没有for循环的函数
def onlyfunction(x):
    newstring = x.capitalize()
    outputstring = x + " " + newstring
    print(outputstring)

And a function with a loop inside the for.

# 创建一个函数,其中 for 循环在函数内部
def forinfunction(listofstrings):
    for x in listofstrings:
        newstring = x.capitalize()
        outputstring = x + " " + newstring
        print(outputstring)

Let us use these functions and see the output.

Three time-saving Python tips!

Obviously, the output results of the two functions are the same.

However, the story does not end here. Let's compare, which one is faster?

Three time-saving Python tips!

As you can see, using a for loop inside a function is slightly faster.

In another project of mine, I need to handle many complex image processing tasks. In comparison, using a for loop inside a function is 1.5 times faster than calling the same function on each iteration. Of course, these are just my personal experiences, but they may be useful if you encounter similar situations.

In short, you can use Python’s built-in functions more. to improve the speed of your Python programs while keeping the code concise and easy to understand.

If you want to know more about Python’s built-in functions, you can refer to the table below, or you can check the website below:

https://www.php.cn /link/3dfe2f633108d604df160cd1b01710db

Three time-saving Python tips!

The above is the detailed content of Three time-saving Python tips!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete