Fluent Python the second chapter sequence consists of an array of learning records

Source: Internet
Author: User

Python built-in sequence types overview

List deduction and builder expressions

List inference is a shortcut to building a list, and builder expressions can be used to create sequences of any other type

Example of a list deduction

 for  in range (3)]>>>test[0,2,4]

The general principle of using list derivation is that you can only list derivations to create new lists and keep them as short as possible.

There may be a variable leakage problem in list derivation in Python2, and Python3 solves the problem of variable leakage

Comparison of list derivation and Map/filter

Builder expression

Although you can also use list inference to initialize tuples, arrays, or other sequence types, a builder expression is a better choice. Because the iterator protocol is followed by the generator expression, you can produce elements individually, rather than creating a complete list, and then passing the list to a constructor. More Memory savings

Initializing tuples and arrays with generator expressions

The use of tuples

Tuples can also be used for records with no field names, in addition to being used as immutable lists

Tuples and records

Tuples are actually records of the data, and each tuple in the tuple holds the data for a field in the record, plus the location of the field.

Tuple unpacking

Tuple unpacking can be used * to help us focus on some elements of the tuple

Tuple unpacking can also be applied in nested structures

Named tuples

Collections.namedtuple is a factory function. Can be used to build a tuple with a field name and a class with a name

In addition to attributes that are integrated from a common tuple, named tuples have their own proprietary properties. _fields,_make,_asdict ()

Tuples for immutable lists

In addition to adding or subtracting elements related methods, tuples support all other methods of the list.

The methods and properties of a list or tuple

Object slices

S[A:B:C] for s between A and B with C as the interval value, C can be negative, reverse value

Multidimensional slices and ellipsis

Slice Assignment

Use + and * for sequences

During stitching, two sequences that are manipulated are not modified, and Python creates a new sequence that contains the same type of data as the result of stitching.

+ and * Follow this rule, not modify the original operand, but build a new sequence

Increment assignment of a sequence

The Special method behind + = is __iadd__ But if a class does not implement the method, Python calls __add__

The special method behind *= is that __imul__ is similar to + =

The lessons that have been learned

Do not put mutable objects in tuples

Incremental assignment is not an atomic operation

It's not hard to see Python bytecode, he's very helpful in understanding the mechanics behind the code.

List.sort methods and built-in functions sorted

List.sort The local sort, sorts the original list and returns none. Direct manipulation of the original list

Sorted is not, a new list is created, and the list is sorted in the original list.

Reverse parameter, true in descending order, false by default

Key parameter used to compare the keywords

Bisect managing sorted sequences

The Bisect module contains two main functions, bisect,insort, and two functions use a binary lookup algorithm to find or insert elements in an ordered sequence

Bisect.insort (Seq,item) Inserts the item into the SEQ and keeps the seq sorted in ascending order

Array

If we need a list that contains only numbers, then Array.array is more efficient than list, and arrays support all operations related to mutable sequences. including Pop,insert and extend. In addition, the array also provides a faster way to read and save files from a file frombytes and ToFile

Properties and methods for lists and arrays

Memory View Memoryview

SciPy and NumPy

bidirectional queues and other forms of queuing

The Collections.deque class is a thread-safe data type that can quickly add or remove elements from both ends.

Methods for list and bidirectional queues

Fluent Python the second chapter sequence consists of an array of learning records

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.