Python learning 3 --- sequence, list, tuples, python ---

Source: Internet
Author: User

Python learning 3 --- sequence, list, tuples, python ---
I. Sequence

1.1 sequence Concept

The most basic data structure in pythn is sequence ).

Each element in the sequence is assigned a sequence number-element index. The first index is 0, the second is 1, and so on. Similar to the subscript In arrays and collections in JAVA.

From the back to the front, the last index is-1,-2 ,....

Python contains six built-in sequences. The list and metadata are the two most common types: String, Unicode string, buffer object, and xrange object.

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

The list is used to add elements.

For some reason, you can use tuples when you cannot modify them.

 

Ii. Sequence

2.1 Sequence Concept

Use [] definitions, such as green = ['test', 'test1', 'test2']

2.2 sequence specified Element operations

A string is also a sequence of characters.

Use len (sequence name) to obtain the sequence length

The last element is len ()-1,

Use the sequence name [index number] to obtain the specified index Element

Use the pop (INDEX) function to delete an element. Delete the last element without writing an index.

Modify the element of a specified cable: sequence name [Index] = New Value

Add new elements at the last position

Sequence name. append (new element)

2.2. Parts

Fragment can operate on elements within a certain range.

Partitions are implemented through two indexes separated by colons.

Example: taget = '<a href = "http://www.163.com"> 163 web sit </a>'

Taget [] is an element starting from subscript 9 to subscript 28.

The first index element is contained in the shard,

The second index is not included in the shard.

Use numbers more obvious

2.3 step size

[Index: Index: Step Size]

When a negative number is used as the step size, the start point must be greater than the end point.

In a positive step, Python extracts elements from the right of the sequence header until the last element;

For a negative step, the element is extracted from the left end of the sequence until the first element.

 

2.4 sequence Addition

Only two sequences of the same type can be joined together. Different types cannot be connected together.

 

2.4 Series

The number x is used to generate a new sequence. In the new sequence, the original sequence will be repeated x times.

None, list, and initialization

An empty list can be expressed in parentheses ([]). If you want to create a list that occupies 10 elements but does not contain any useful content, it can be [42] * 10, or [0] * 10. This generates a list containing 10 0 elements,

However, sometimes a value may be required to represent a null value ------ meaning that no element is placed. In this case, None can be used,

None is a python built-in value. Its exact meaning is "nothing here". Therefore, if you want to initialize a list with a length of 10, you can use [None] * 10,

 

2.5. Membership

Check whether an element is in the sequence. If the in operator is used, False is returned if True is not returned.

2.6, length, maximum value, and minimum value

Len (list name)

Max (list name)

Min (list name)

Iii. List

 

3.1 list Functions

The string cannot be modified like a list, so that the list can split the string into a list with one element for each character. Applicable to all types of sequences, not just strings.

3.2 change list: Element assignment

Use the list [Index] = new value.

3.3. Change the list: delete an element

Sequence can be deleted using pop (INDEX)

Here, del list name [Index] is used to delete

 

3.3 change list: multipart assignment

Use the list name [index: Index] = New Value


You can use step size to assign values, positive and negative step size.

 

3.3 list: Common Methods

1. append () method is used to append a new object to the end of the list: List. append (new element)

2. count the number of times an element appears in the list: List. count (value)

3. The extend method can add multiple values in a sequence at a time at the end of the list.

4. The index method is used to locate the index list of the first occurrence of a value from the list. index (value)

5. The insert method is used to insert objects to the list. insert (index, value)

6. The pop method removes an element from the list. By default, it is the last element and returns the value of this element. List. pop ();

7. The remove method is used to remove the first match of a value in the list. List. remove (value)

8. Elements in the reverse method list are stored in reverse order.

9. sort the list

10. advanced sorting. If you need to sort by specific method rather than the default sort method, you can use compare (x, y) for custom comparison.

Compare (x, y) returns a negative number in x <y

Compare (x, y) returns a positive number in x> y.

Compare (x, y) returns 0 if x = y

11. sort: The method has two other optional parameters, key-reverse. If you use them, you must specify them by name,

The parameter key is similar to cmp. It must be provided. The function used in a sorting process is not used to determine the object size, but to create a key for each element, then all elements are sorted by key. Therefore, if the elements are sorted by the length, len can be used as a function.

 

12. reverse: A simple boolean value (True or False) that specifies whether the list is sorted in the back direction.

 

 

 

 

Iv. tuples

 

4.1. Concepts of tuples

The list of tuples is also a sequence. The only difference is that the tuples cannot be modified, and the same is true for strings.

Create tuples. Syntax: Use commas (,) to separate values to automatically create tuples. It is best to use parentheses.

Use (,) to declare

4.2. tuple Function


 

4.3 basic tuples

Because it cannot be modified, only access operations are available.

 

 

5. functions used

Cmp (x, y): Compares two values.

Len (seq): sequence length

List (seq): converts a sequence to a list.

Max (args): returns the maximum value in a sequence or set of parameters.

Minx (args): returns the minimum value in a sequence or set of parameters.

Reversed (seq): reverse iteration of object Sequences

Sorted (seq): returns a list Of all sorted elements including seq.

Tuple (seq): converts a sequence to a metagroup.

 

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.