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 important Python skills

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what are the important Python skills". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the important Python skills"?

1. Ramda function (Lambda Functions)

The Ramda function is very powerful. Of course, we don't use it when we have to clean up multiple columns in the same way, but this is not often the case. Typically, each property requires its own logic after cleaning up.

The Lambda function allows you to create anonymous functions. This basically means that specific functions can be generated quickly without having to use pythonsdef to define functions correctly.

Keep in mind, however, that the Lambda function is mainly designed as one-liners, so it should be used for simple things. For more complex logic, regular functions are required.

We will show two concrete examples through which we do not need to define functions for all projects, thus saving a lot of time. Although the first example may not be commonly used in reality, it is worth mentioning. This is to square the numbers.

# regular function def square_number (x): res = x * * 2 return res# lambda function square = lambda x: X * * results print ('square_number (4): {}' .format (square_number (4) print ('square lambda: {}'. Format (square (4)) > > square_number (4): 16 > > square lambda: 16

The above code snippet implements the same logic in the normal way and in the way of the lambda function. Although the result is the same, lambda's single line looks much more comfortable!

The second example is about checking whether the number is even or non-even:

# regular function def is_even (x): if x% 2 = 0: return True else: return False # lambda function even = lambda x: X% 2 = = results print ('is_even (4): {}' .format (is_even (4)) print ('is_even (3): {}' .format (is_even (3)) print ('even (4): {}' .format (even (4)) ) print ('even (3): {}' .format (even (3)) > > is_even (4): True > > is_even (3): False > even (4): True > even (3): False

Once again, the same logic is implemented in two ways. You decide which one you like.

2. List parsing (List Comprehensions)

Simply put, list parsing allows us to create lists using other symbols. You can think of it as a single-line loop in parentheses.

When doing feature engineering, it is convenient to use list parsing. For example, suppose we are doing spam detection by analyzing e-mail headers, then we will want to know if question marks often appear in spam. If you do it with list parsing, this will be a very simple task.

There will be no more theoretical explanations. Examples are the most important thing.

The example here chooses to declare a regular function that checks for items in the list that begin with a character ("a" in this case). After implementation, perform the same operation with list parsing. Guess which one will write faster?

Lst = ['Acer',' Asus', 'Lenovo' 'HP'] # regular function def starts_with_a (lst): valids = [] for word in lst: if word [0] .lower () =' asides: valids.append (word) return valids # list comprehension lst_comp = [word for word in lst if word [0] .lower () = ='a'] # results print ('starts_with_a: {}' .format (starts_with_a ( Lst)) print ('list_comprehension: {}' .format (lst_comp)) > starts_with_a: ['Acer' 'Asus'] > list_comprehension: [' Acer', 'Asus']

If this is the first time you have seen this way, the grammar may be a little confusing. But when you write such functions every day, they will attract you more and more to see how many complex things you can apply.

3. Zip function

This is one of the built-in python methods that are rarely seen in practice. From a data scientist's point of view, it enables us to iterate through two or more lists at the same time. This can come in handy when dealing with dates and times.

For example, when one attribute represents the start time of an event, and the second attribute represents the end time of the event, it is almost always necessary to calculate the time difference between them for further analysis. By far, the zip function is the easiest way.

For example, compare the weekly sales dates of some fictional companies and fictional regions:

Sales_north= [350,287,550,891,241,653,882] sales_south = [551,254,901,776, 105,502,976] for S1, S2 in zip (sales_north,sales_south): print (S1-S2) > >-20133-351 115 136 151-94

Let's see how easy it is. You can apply the same logic to iterate over three arrays at the same time, just add "S3" and some other list names in parentheses.

Thank you for your reading, the above is the content of "what are the important Python skills". After the study of this article, I believe you have a deeper understanding of the important Python skills, 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