Basic Python Tutorial "reading notes"-2016/7/14

Source: Internet
Author: User

Hope to continue to update through the blog park, share and record the basic knowledge of Python to the advanced application of the dots drop!

Wave Six: The 2nd Chapter

[Overview] List and tuples

Data structures are a collection of elements that are organized in some way together, and data elements can make numbers or strings, or even other data structures. The most basic data structure is the sequence sequence.

Each element in the sequence is assigned an ordinal---the position of the element, also known as an index. The first index is 0, the second one is 1, and so on.

First, an overview of the sequence, followed by the operations that are common to all the sequences, and the same applies to strings. Learn how to use a list and see what's special about it. Then we discuss tuples, which are similar in nature and lists, except that they cannot be changed.

[2.1] Sequence Overview

  Python contains 6 built-in sequences that focus on the two most common types: lists and tuples. Other built-in sequence types are strings, Unicode strings, buffer objects, and Xrange objects.

The main difference between a list and a tuple is that the list can be modified and tuples cannot.

Python also has a data structure called container container. A container is basically any object that contains other objects. Sequences and mappings are two main types of containers. Each element in the sequence has its own number, and each tuple in the map has a name (also called a key). As for container types that are neither sequential nor mapped, the set is an example.

[2.2] General sequence operation

  Action: Index indexing, shard sliceing, add adding, multiply multiplying, and check whether an element belongs to a member of a sequence (membership). There are also built-in functions that calculate the length of a sequence and find the largest and smallest elements.

There is also an action---iterative iteration, which iterates over a sequence by repeating some operations on each element of the sequence at once.

[2.2.1] Index

All elements in a sequence are numbered---increment from zero. Elements can be accessed individually by number. Gets an element by index. When a negative index is used, it is counted from the right, that is, from the last element. The position number of the last 1 elements is-1 instead of 0, because that will coincide with the 1th element.

[2.2.2] Shard

A shard operation to access an element within a certain range. Shards are implemented by two indexes separated by a colon. The 1th index is the number of the 1th element that needs to extract the part, and the last index is the number of the 1th element that is left after the Shard. In short, the implementation of a shard operation needs to provide two indexes as the boundary, the element of the 1th index is contained within the Shard, and the 2nd is not contained within the Shard.

1. An elegant shortcut

If the portion of the Shard includes the element at the end of the sequence, you only need to empty the last one so you can.

If you need to assign an entire sequence, you can empty all two indexes.

2. Larger step size

The start and end points of a shard need to be developed, while the other parameter---step SETP length is usually implicitly set. The default step is 1, and if the step is set to a number larger than 1, then some elements are skipped. The step size can be a complex number, that is, from right to do extracting elements, and must have the start point greater than the end point.

[2.2.3] Series Think home

You can sequence a connection operation by using the plus sign. Two sequences of the same type are required for connection operations.

[2.2.4] Multiplication

None, empty list, and initialization

An empty list can be represented simply by two brackets, but if you want to create a list that takes up 10 elements and does not include any content, you need to use none. None is a python built-in value that can be used to initialize the list with none.

[2.2.5] Membership

Checks whether a value is in a sequence and can use the in operator.

[2.2.6] length, minimum and maximum values

The built-in function len, Min, Max.

[2.3] List: Python's "toil"

  The strength of the list is self-evident. The list is mutable and can change the contents of the list.

[2.3.1] List function

Because a string cannot be modified like a list, it is sometimes useful to create a list from a string, which can be done by the list function. You can convert a list to a string using the following code:

'. Join (Somelist)

[2.3.2] Basic list operations

Lists can use indexes, shards, joins, and multiplication. The list can be modified.

Describes how to change a list: element assignment, deletion, shard assignment, and list methods. But note that not all of the list methods are really changing the list.

1. Assigning values to elements

Use index tags to assign values to a specific, well-positioned element. You cannot assign a value to an element that does not exist in a location.

2. Deleting an element

Use the DEL statement to implement

del List[index number]

At this point the deleted element is completely gone. Del can be used for delete operations of dictionary elements and even other variables.

3. Shard Assignment

Shard assignment operations are more powerful. You can replace a shard with a sequence that is not equal to the original sequence. A shard assignment statement can insert a new element without replacing any of the original elements.

numbers[1:1]=[2,3,4]

This program simply replaces an empty shard, so the actual operation is to insert a sequence. And so on, it is also possible to delete elements by assigning values to a shard operation.

[2.3.3] List method

Method is a function that is closely related to certain objects, which may be lists, numbers, or strings or other types of objects.

The method can be called as follows:

Object. Method (Parameter)

The object is placed before the method name and is separated by a dot number.

1. Append

The Append method is used to append a new object to the end of the list. The Append method simply modifies the original list in the appropriate location, and it modifies the original list directly.

2. Count

The Count method counts the number of occurrences of an element in the list.

3. Extend

The Extend method can append more than one value from another sequence at the end of the list. The existing list is expanded with the new list. The Extend method modifies the extended sequence, while the original connection operation returns a completely new list. The original connection operation created a new list. If the operation shown below is required, then the connection operation will be less efficient than the Extend method.

A=a+b

4. Index

The index method is used to find the position of the index of the first occurrence of a value from the list.

5. Insert

The Insert method is used to insert an object into the list.

6. Pop

The Pop method removes an element from the list, defaults to the last one, and returns the value of the element.

The Pop method is the only way to modify the list and return the list of element values.

A common data structure---stack can be implemented using the Pop method. The stack's principle is like stacking a plate, only to put a plate on top, also can only take a plate at the top. The last to be put into the stack is first removed (this principle is called LIFO, which is LIFO). For the above two stack operations, called into the stack (push) and out of the stack (POP). Python can use the Append method instead of the stack-in method. The action of the pop method and the Append method is exactly the opposite.

X.append (X.pop ())

If you need to implement a FIFO queue, you can use Insert instead of the Append method. A better solution is to use the Deque object in the collection module.

7. Remove

The Remove method is used to remove one of the items in the list that is worth the first match.

8. Reverse

The reverse method stores the elements in the list in reverse. The method also changes the list but does not return a value. If you need to reverse iterate over a sequence, you can use the reversed function. Instead of returning a list, this function returns an Iterator object.

9. Sort

The sort method is used to sort the list at the original location. means changing the original list so that the elements can be sorted in a certain order, rather than simply returning a sorted list copy.

The problem arises when the user needs a copy of the ordered list while keeping the original list intact. The following are the wrong practices:

x=[4,3,2,1,5,6]

Y=x.sort ()

Print Y # None

Because the sort method modifies x but returns a null value, the last result is a sorted x and Y with a value of none. The correct way to do this is to first assign a copy of x to Y, and then sort Y. As shown below:

x=[4,3,2,1,5,6]

y=x[:]

Y.sort ()

Calling x[again:] Gets a shard that contains all the elements of X, which is a very efficient way to assign an entire list. Simply assigning x to Y is useless, because doing so allows X and Y to point to the same list.

[2.4] Tuples: Immutable sequences

  Tuples cannot be modified. The syntax for creating tuples is simple: if you separate some values with commas, tuples are created automatically. It is important to have commas in tuples, and it is useless to just add parentheses.

[2.4.1] Tuple function

Take a sequence as an argument and convert it to a tuple.

[2.4.2] Basic element operation

Create tuples and access elements.

[2.4.3] So, what's the point?

Tuples are not replaceable:

Tuples can be re-mapped (and members of the collection) used as keys;

Tuples exist as return values for built-in functions and methods.

[2.5] Summary

sequence: data structure.

Membership:The in operator checks whether a value is in a container.

method: method is an important concept of object-oriented programming.

  

  

  

Basic Python Tutorial "reading notes"-2016/7/14

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.