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

What are the tips for efficient Python code?

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

Share

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

This article mainly explains "what Python efficient code tips", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "what Python efficient code tips"!

Combine uncommon categories into one

Sometimes you get columns with uneven distribution of elements, and rare categories are just there. It is often desirable to be able to combine these categories into one.

df.artists.value_counts()

Coldplay and Weekend are combined into one category because they have minimal impact on the dataset. What should I do?

First, find the elements you don't want to change, such as Eminem, TaylorSwift, and BrunoMars:

myList =df.artists.value_counts().nlargest(3).index

Replace other elements with the where() function

dfdf_new = df.where(df.artists.isin(myList),other='otherartists') df_new.artists.value_counts()

This is the updated column modified as required.

Find new elements in a list

Given two different lists, when you want to find elements that are present in one list but not in the other, refer to both lists:

A = [ 1, 3, 5, 7, 9 ] B = [ 4, 5, 6, 7, 8 ]

To find a new element in list A, we take the set difference between list A and list B:

set(A) - set(B)

Values 1, 3, and 9 appear only in List A and not in List B.

Get rid of warnings

When you run code, you often get a lot of warnings. It didn't take long for it to start irritating. For example, you may receive a FutureWarning message whenever you import a dynasty

You can hide all warnings with the following code. Make sure it is written at the top of the code.

import warnings warnings.filterwarnings(action='ignore') import keras

This will help hide all warnings throughout the code.

Map() function

The map() function takes two parameters, function and iterable, and returns a map containing the result:

map(func,itr)

func is a function that receives a given sequence element from a mapping pass.

ITr refers to sequences that can be mapped.

def product(n1,n2): return n1 *n2 list1 = (1, 2, 3, 4) list2 = (10,20,30,40)result = map(product, list1,list2) list(result)

Start decoding.

The Product function takes two lists and returns the product of the two lists. List 1 and List 2 are two lists that act as map function sequences. map() sets the product function and sequence in one → List 1 and List 2, and feeds back the product of the two lists as the result.

Map + Lambda

The above code can be modified to replace the product function with lambda expressions:

list1 = (1, 2, 3, 4) list2 = (10,20,30,40) result = map(lambda x,y: x * y, list1,list2) print(list(result))

Lambda expressions help reduce the cost of writing functions separately.

Start, Stop and Setup

Slice(start:stop[:step]) is an object that usually contains a partial sequence.

If only stops are provided, partial sequences are generated starting at index 0 until stops.

If only the start is provided, partial sequences are generated after the index start up to the last element.

If both start and stop are provided, a partial sequence is generated after the index starts until it stops.

If start, stop, and step are provided at the same time, a partial sequence is generated after index start until stop, and index step is added.

x = [ 1, 2, 3, 4, 5, 6, 7, 8 ] x[ 1: 6: 2]

In the above code, 1 is the start index, 6 is the stop index, and 2 is the step index. This means starting at index 1 and stopping at index 6 in steps of 2.

You can also flip the list using the [::-1] operation:

x[::-1]

Yes, it's easy to reverse the entire list by starting, stopping, and stepping.

Zip and Enumerate

The zip and enumerate functions are often used in for loops, but they are even better when used together. Not only can it iterate over multiple values in a single loop, but it can also obtain indexes simultaneously.

NAME = ['Sid','John','David'] BIRD = ['Eagle','Sparrow','Vulture'] CITY =['Mumbai','US','London']for i,(name,bird,city) inenumerate(zip(NAME,BIRD,CITY)): print(i,' represents ',name,' ,',bird,' and ',city)

The Zip function merges all lists into one so that each list can be accessed at the same time, while the Enumerate function assists in obtaining the index and the elements attached to it.

random sampling

Sometimes very large data sets are encountered and it is decided to process a random subset of the data. The sample function of the pandas data frame can do more. Take a look at the singer data model you created above.

df.sample(n=10)

This helps to get 10 random rows in the dataset.

df.sample(frac=0.5).reset_index(drop=True)

Break down the code above, and the frac parameter takes values between 0 and 1, inclusive. It occupies a portion of the data stream allocated to it. 0.5 is specified in the code snippet above, so it returns a random subset of size→0.5*

You can see the reset_index function above. It helps to rearrange the index appropriately, since the index is also rearranged when a random subset is obtained.

reserved memory

As you program deeper, you will realize the importance of remembering memory-efficient code. Generators are functions that return objects that we can traverse. This helps make efficient use of memory, so it is mainly used when iterating over infinite sequences.

def SampleGenerator(n): yield n nn = n+1 yield n nn = n+1 yield ngen = SampleGenerator(1)

The Yield statement suspends the function, saves all its state, and continues execution on subsequent successive invocations.

print(next(gen)) print(next(gen)) print(next(gen))

As you can see, yield preserves the previous state, and whenever we call the next function, it continues to the next yield that returns its new output.

A single yield can be iterated over by adding a while loop that runs indefinitely inside the generator function.

def updatedGenerator(n): while(1): yield n nn = n + 1 a = updatedGenerator(1)for i in range(5): print(next(a))

While statements can iterate over the same yield statement.

Savior Skiprows

The grand finale! The csv file you want to read is so large that you don't have enough memory? Skiprows can be easily solved.

It specifies the number of rows to skip in the data frame.

Suppose you have a data set of 1 million rows that doesn't fit in your memory. If you assign skirows =0.5 million(skip 500,000 rows), 500,000 rows are skipped when reading the dataset, making it easy to read subsets of the dataset.

df = pd.read_csv('artist.csv') df_new = pd.read_csv('artist.csv',skiprows=50)df.shape, df_new.shape

In the code snippet above, df represents a dataset of 112 rows. After adding skirows =50(skipping 50 rows), it skips 50 rows in the dataset, reading 62 rows as a new dataset.

At this point, I believe that everyone has a deeper understanding of "what Python efficient code tips", you may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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