Home  >  Article  >  Backend Development  >  Four Python deductive development techniques to make your code more efficient

Four Python deductive development techniques to make your code more efficient

WBOY
WBOYforward
2023-04-22 09:40:081227browse

For data science, Python is usually widely used for data processing and transformation. It provides powerful data structure processing functions to make data processing more flexible. What does "flexibility" mean here?

This means that there are always multiple ways to achieve the same result in Python, we always have different methods and need to choose the one that is easy to use, saves time and gives better control.

It is impossible to master all these methods. So here is a list of 4 Python tips you should know when working with any type of data.

Four Python deductive development techniques to make your code more efficient

List comprehension List comprehension is an elegant and most Python-friendly way to create lists. Compared to for loops and if statements, list comprehensions have a much shorter syntax for creating a new list based on the values ​​of an existing list. So let's see how this feature gets a copy of the list.

Copying a list using list comprehensions

Sometimes you need to create a copy of an existing list. The simplest answer is .copy(), which allows you to copy the contents of one list to another (new) list.

For example, a list of integers original_list.

original_list = [10,11,20,22,30,34]

This list can be copied simply using the .copy() method.

duplicated_list = original_list.copy()

List comprehensions can get exactly the same output. Copying a list is a great example of understanding list comprehensions.

Look at the code below.

duplicated_list = [item for item in original_list]

This is not to say that it is better to use list comprehensions when copying lists, but it is to say that this case is the best for introducing the working method of list comprehensions.

Next, let’s see how list comprehensions make life easy when performing mathematical operations on each element of a list.

Multiply the elements in the list

The simplest or direct method of multiplication is to use the multiplication operator, which is *

For example, if you want to use a scalar (i.e. the number 5) Multiply each item in the list. You definitely can't use original_list*5 here because it will create 5 copies of the list.

In this scenario, the best answer is a list comprehension, as shown below.

original_list = [10,11,20,22,30,34]
 multiplied_list = [item*5 for item in original_list]
 
 # Output
 [50, 55, 100, 110, 150, 170]

The operation here is not limited to multiplying a number. Complex operations can be performed on each element of the original list.

For example, suppose you want to calculate the cube of the square root of each term. You can solve it in one line.

multiplied_list = [math.sqrt(item)**3 for item in original_list]
 
 # Output
 [31.6227766016838,
36.4828726939094,
89.4427190999916,
103.18914671611546,
164.31676725154983,
198.25236442474025]

The function sqrt used to calculate the square root of a number belongs to the library math, so you need to import it before using it in this example.

Similar to the built-in functions shown above, it is also possible to use user-defined functions on each element of the list.

For example, the simple function shown below.

def simple_function(item):
item1 = item*10
item2 = item*11
return math.sqrt(item1**2 + item2**2)

This user-defined function can be applied to each item in the list.

multiplied_list = [simple_function(item) for item in original_list]
 
 # Output
 [148.66068747318505,
163.52675622050356,
297.3213749463701,
327.0535124410071,
445.9820624195552,
505.4463374088292]

List comprehensions are even more useful in practical scenarios. Usually in analysis tasks you need to remove certain types of elements from a list, such as eliminating nan elements. List comprehensions are the perfect tool for these tasks.

Deleting elements from a list

Filtering data based on specific criteria is one of the common tasks of selecting a desired data set, and the same logic is also used in list comprehensions.

Suppose you have the list of numbers mentioned below.

original_list = [10, 22, -43, 0, 34, -11, -12, -0.1, 1]

You want to keep only positive values ​​from this list. So logically you want to keep only those items that evaluate to TRUE for conditional items > 0.

new_list = [item for item in original_list if item > 0]
 
 # Output
 [10, 22, 34, 1]

The if clause is used to delete negative values. You can apply any condition using if clause to remove any item from the list.

For example, when you want to delete all items whose square is less than 200, all you need to do is to mention the conditional item **2 > 200 in the list synthesis, as shown below.

new_list = [item for item in original_list if item**2 > 200]
 
 # Output
 [22, -43, 34]

When dealing with real data sets, the conditions for filtering list items may be much more complex, this method is fast and easy to understand.

Use dict() to convert two lists into dictionary key-value pairs

Sometimes you need to create a dictionary from the values ​​in two lists. Instead of typing them in one by one, you can use dictionary comprehensions (dictionary comprehension), which is an elegant and concise way to create a dictionary!

It works exactly like a list comprehension, the only difference is - when creating a list comprehension, you wrap everything in square brackets, such as [], whereas in a dictionary comprehension, you wrap everything Enclosed in curly braces, such as {}.

Suppose there are two lists - fields and details - as shown below.

fields = [‘name’, ‘country’, ‘age’, ‘gender’]
 details = [‘pablo’, ‘Mexico’, 30, ‘Male’]

A simple way is to use a dictionary comprehension like this -

new_dict = {key: value for key, value in zip(fields, details)}
 
 # Output
 {'name': 'pablo', 'country': 'Mexico', 'age': 30, 'gender': 'Male'}

The important thing to understand here is how the function zip works.

In Python, the zip function accepts iterable objects such as strings, lists, or dictionaries as input and returns them aggregated into tuples.

So, in this case zip has formed a pair of each item from the list fields and details. When using key:value in a dictionary comprehension, simply unpack this tuple into individual key-value pairs.

This process even gets faster when using the built-in dict() constructor in Python (for creating dictionaries), since dict() is at least 1.3 times faster than dictionary comprehensions!

So we need to use this constructor with the zip() function, its syntax is much simpler - dict(zip(fields, details))

Summary

As As I mentioned at the beginning, Python is very flexible as there are multiple ways to achieve the same result. Depending on the complexity of the task you need to choose the best way to achieve it.

I hope this article can be useful to you. If there is any other way to do the same thing I mentioned in this article, please let me know.

The above is the detailed content of Four Python deductive development techniques to make your code more efficient. 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