Python sequence object basis ---

I. Introduction sequence

  • Is a collection of data structure elements are grouped together in some way.
  • The container (Container) Python is a data structure, comprising a substantially any object other objects. Sequence and mapping (e.g., dictionary) are two main container. Collection (Set) is the container type.
  • Sequence is the most basic data structures, a collection of data elements through the data element ID to organize them together.
  • Ordered arrangement of members, it can be accessed by several members of a small scale or offset, referred to as type sequence such python.
  • Python has built-in types of sequence 6: list of tuples, strings, Unicode strings, buffer objects, xrange object.

 The official document:

sequence

Translation:
an iterator object that supports through the __getitem __ () special method integer efficiently access the element index, and defines a __len __ () method, which returns the length of the sequence. Some built-in sequence types are list, str, tuple and bytes. Note, dict also supports __getitem __ () and __len __ (), but it is considered a mapping rather than a sequence because the lookup arbitrary immutable keys rather than integers.

collections.abc.Sequence abstract base class defines a ratio of the __getitem __ () and __len __ () richer interfaces, increases the count (), index (), __ contains __ (), and __reversed __ (). Can register () explicitly register achieve this type of extension interface.

II. Classification sequences

Classification sequence

 

 Note:

  • The difference between the container and the flat sequences that sequence, which flat element type sequence is the same
  • array module is a highly efficient memory array type implemented python. It is similar list, but all the members of the array must be of the same type, in the creation of the array, to determine the type of the array
  • deque class is the python standard library collections in one module, which provides a sequence of both sides can operate, which means that, before and after the execution sequence of you can add or delete.

III. Operation general sequence

1. index (Indexing)

• Each element in the sequence is assigned a number, i.e., the position of the element, called an index. Index of the first element to a positive number is 0, the second element index of a positive number is 1, the inverse of the first element index is -1, and so on.

2. slice (Slicing)

• slice using two three numbers separated by colons accomplished: [srart: end: step]

• the first number indicates the start position of the slice (the default is 0), the second number indicates slices off (but not including) position (default list length), the third number represents the slice step (default 1)

• You can use slices to intercept any part of the list to get a new list, you can also modify and delete some elements of the list by the slice, even adding elements to the list of objects by slicing operation.

• using different ways to access the list of index elements, the operation will not slice subscript out of bounds and throw an exception, but simply cut off the end of the list or return an empty list, the code more robust.

When • step is positive, Python can be extracted from the sequence header start right elements until the last element, the element of the index should start at the left end of the index element, otherwise it will return an empty sequence; when the step is negative, Python will from end of the sequence to start the extraction elements to the left until the first element, then the element index should start on the right end of the index element, otherwise it will return an empty sequence. step can not be 0.

• When the last step can be omitted omitted a colon or a blank last index

[start:end]或[start:end:]

• When the blank to start or end time corresponding to the index will be omitted

[Start:] or [: End] or [:]

• Slice can return a list of shallow copy of a [:] == a 

• a [:] will give a fragment comprising all elements, is very efficient method for copying the entire list

3. Add (Adding)

• serial connection operation, only the same type of sequence can connect operation

• essentially create a new sequence and the original sequence elements and new elements in turn copied to the new sequence memory space

4 by (Multiplying)

• Repeat the sequence, with the number x multiplied by a sequence will generate a new sequence, the new sequence is a repeat of the original sequence

5. Membership

• using the in operator (Boolean operators) to check whether an element in the sequence, returns a Boolean value True or False

>>> 2 in [2,3,4]

True

>>> [2] in [2,3,4]

False

>>>‘P’in ‘PYTHON’

True

6. Built-in Functions

• len (): Returns the number of elements in the sequence

• max (), min (): returns the minimum or maximum sequence element

7. iteration (iteration)

8. Assignment, shallow copy and deep copy

① direct assignment: in fact, object reference (alias).

b = a: assignment references, a and b are the same object.

② shallow copy (copy): copy the parent object, the child object will not be copied inside of the object.

b = a.copy (): shallow copy, a and b are a separate object, sub-object, or they point to a unified object (reference).

If you place editing sub-objects, the parent object will change.

③ deep copy (deepcopy): deepcopy method copy module, complete copy of the parent object and child objects.

b = copy.deepcopy (a): a deep copy, a and b complete copy of the parent object and its children, the two are completely separate.

Guess you like

Origin www.cnblogs.com/nlkblog/p/11532204.html