Some built-in functions you should know in Python, Python built-in functions

Source: Internet
Author: User

Some built-in functions you should know in Python, Python built-in functions

Preface

Python has some very clever and powerful built-in functions. For Beginners, it is generally not used very much. I also found out after using python for a while. Wow, there are such good functions, this function is classic and strictly tested. It saves you a lot of time, and the code is simple and easy to read. it facilitates both yourself and reduces bugs.

I. sorted ()

1) sort a list

sorted([100, 98, 102, 1, 40])>>>[1, 40, 98, 100, 102]

2) Pass the key parameter/Function

For example, a long list contains many dictionary elements, which are sorted by the length of each element.

L = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}]new_line=sorted(L,key=lambda x:len(x))print(new_line)>>>[{1: 9}, {1: 5, 3: 4}, {1: 3, 6: 3}, {1: 1, 2: 4, 5: 6}]

3) sort the List composed of tuple

For example, the following is a list of ages of students.

students = [('wang', 'A', 15), ('li', 'B', 12), ('zhang', 'B', 10)] print(sorted(students, key=lambda student : student[2])) >>>[('zhang', 'B', 10), ('li', 'B', 12), ('wang', 'A', 15)]

4) sort by cmp Functions

students = [('wang', 'A', 15), ('li', 'B', 12), ('zhang', 'B', 10)] print(sorted(students, cmp=lambda x,y : cmp(x[0], y[0])) )>>>[('li', 'B', 12), ('wang', 'A', 15), ('zhang', 'B', 10)]

In fact, the sorting of python needs to be carefully discussed. It takes a whole space to talk about its sorting algorithm. There is a lot of content. If you are interested, you can look at the source code to see how it is designed, click here first.

2. map ()

Map maps a specified Sequence Based on the provided functions. It accepts a function f and a list, and uses function f to act on each element of the list, then a new list is returned. The input parameters of the map function can also be multiple. note that this function must have a return value (the value is important three times ).

Otherwise, a new list similar to [None, None] will be returned.

A suitable scenario is to perform repeated operations on some elements in the list, which can be easily done using map.

3. enumerate ()

In Python, iterations always retrieve the element itself, rather than the element index. Sometimes we need to know the element index. For example, in a long list, there are some website names, we hope that indexes can also be listed during printing. Without this function, we need to add a variable to increase the number of counting variables during loop printing. Now with enumerate, you don't have to worry about it.

Iv. zip ()

The zip function accepts any number of (including 0 and 1) sequences as parameters and returns a tuple list.

x = [1, 2, 3]y = [4, 5, 6]z = [7, 8, 9]xyz = zip(x, y, z)>>print xyz

This function is especially convenient when building dictionary sequences (this is a clever trick, so you can try it out carefully)

5. filter ()

The filter function accepts a function f and a list. The function f is used to determine each element and returns True or False. In this way, some elements that do not meet the conditions can be filtered out, then return the list that meets the condition.

def is_even(x): return x%2==0print(filter(is_even,[1,2,3,4,5]))>>>[2, 4]

Especially when processing files, we need to remove some spaces, carriage return and null characters.

6. reduce ()

The reduce function is similar to map. It is also a function f and a list, but the function entry parameters must be two. reduce also calls each element repeatedly, returns the final value, while map returns a list.

Note:In python3, reduce has been removed from the global function. to use it, you need to import reduce from functools.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.