Take you quickly understand the tuple Python3

Python3 tuples work as follows:

file

Tuple (tuple) is a static (the immutable) or is immutable (unchangeable) data structure, which elements are arranged in a certain order. It is static, so the tuple elements can not be modified.

Tuple for a data packet. Each tuple element called "Item" (item).

Value tuples are packaged in parentheses and separated by commas, can be written as empty tuple coral = (), but only one tuple element must use a comma: coral = ( "blue coral",).

If the print function Print tuple (), get the following output, which is still tuple '()' wrapped in parentheses.

file

When you are trying to understand in Python tuple or a set of other types of data structures, to think about a different set of computers that exist is helpful: Arrange different types of files, your list of songs, browser bookmarks, you e-mail, and so you get a collection of video in streaming media.

Tuples and lists like, but the values ​​in the tuple are not modified. Precisely because of this, when you use a tuple in the code, it means that you do not want to tell others the data set is modified. Meanwhile, since the value of the tuple immutable, as compared to the use of lists, your code optimized through the use of tuples.

1, tuple index

As an ordered set of elements, any element of a group can be individually accessed by an index.

Each of which is associated with an index number, which is an integer value, the count starts from 0.

For coral tuple, its index is as follows:

file

The first tuple, the string "blue coral" index is 0, the last element of the tuple is "elkhorn coral", the index number is 3.

Because each index has a corresponding tuple, so we can individually access each element.

Now we access an element by index numbers alone:

file

As shown in the above table, the index numbers range from 0 to 3, so we had access to each element of the index number by:

file

If we use this visit than a tuple index number 3, it will suggest that the index out of bounds.

file

In addition to the index number can be positive, negative numbers may also be, which is counting down from the rear forward, starting at -1.

When we went to visit last several specific tuple of a long, negative form of index number is valid.

Or for coral tuple, its negative form of index numbers are as follows:

file

So if you want to print out the "blue coral" by negative form of an index that can be written as follows:

file

We can also be tuple string value and another string connected by '+' operator:

file

We value and the index number of string elements 1 "This reef is made up of" together. We can also use the '+' to connect two or more tuples.

Each has a corresponding index number tuple, we can easily access to every element tuple.

2, slice tuple

We use an index number to access an item tuple. Slice (Slice) is configured by a range index number: the plurality of values ​​may be obtained in the tuple [x y] simultaneously.

Suppose we want to get in the middle of coral tuple values, it may be constructed as a slice.

file

When we in the configuration: [1 3] the slice, the first place represents a start slice (including the index number), where the second number indicates the end of the slice (not including the index number), and therefore [1: 3 ] can be taken as an index number of the elements 1 and 2.

If you want to get all the elements in addition to the remaining elements of the endpoints, you can [x: y] ignore an item in.

If you want to print out the first three elements coral tuple, you can written as:

file

This print out the information tuple of the front part, stopped before the index number of the element 3.

To print out the information tuple back portion, can be ignored [X: y] values ​​of y:

file

We can also use the negative form of construction slice index number to access the tuples:

file

The last slice is a variable step size (a stride of), which means that when we removed a front how many elements are moved to remove a value.

Several previous examples, we have ignored this variable, use the default value of 1. So we were able to get all the elements in the middle of two index number.

Complete slice builder manner [x: y: z], z refers to a step. We constructed a little longer tuple by step size of the slice 2 to access it:

file

We construct sections [1: 11: 2] value is an index number of prints from 1 (including the index number), the index number of 11 (this does not include the index number), the step of printing a spacer 2 tells us One.

We can ignore the first two sections, the use of only step variables:

file

Can be printed out by the slice length is set to 3 steps, each index number divisible by three elements:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12

By using the positive or negative form of sections, and setting step, we can obtain the values ​​in the tuple more targeted manner.

3, and replicate tuple

Operator can be used to connect or copy tuple. Connection is accomplished by '+' to complete replication is '*'.

'+' Can be connected together two or more tuples. We can merge two values ​​in the tuple get a new tuple.

file

The output is:

file

Because the '+' operator can be used to connect, it may be used to form a plurality of element compositions and new tuple, however, we can not change the tuple one that already exists.

'*' Operator can be used to copy the tuple, you may need to file in a directory on the server to copy all or you want to share a playlist with friends, in these cases, you need to copy a set of data.

Let's coral tuples multiplied by 2, kelp tuples multiplied by 3, to create a new tuple.

file

file

By '*' operator, we can copy a specified number of tuples, to create new tuples original data sequence.

Existing tuple can be '+' or '*' to connect through or copy operation.

4, the function tuple

You can use some built-in functions to manipulate tuples.

4.1 only ()

And a list of similar strings, we can () function is calculated by the length len tuples, tuples passed as arguments:

file

When you want a different set maximum or minimum length, this function is very useful.

When a function len kelp tuples and numbers, we get the following output:

file

We get the output because kelp has four elements, numbers 13 elements:

file

Examples of the length of the tuples is small, when the tuple long len () of the specific number of elements can be obtained.

4.2 max () and min ()

When dealing with numerical tuple (including integer and floating point), we can use max () and min () function to find the maximum and minimum values ​​for a particular tuple.

These two functions allows us to get information on quantitative data, such as test scores, temperature, and prices.

Look tuple consisting of floating-point numbers:

file

To use the max () function, the need to transfer a tuple into the function, max (more_numbers).

max () function can be obtained the maximum tuple.

Similarly, we can use the min () function to get the minimum.

file

So we can find and print out the minimum tuple.

And len () function, as when a large data item tuple, max () and min () is useful.

5, tuples and lists is different from the

The primary differences between tuples and lists is the value of the tuple can not be changed. This means you can not add or delete tuple value, the value of a data item can not be modified.

However, we tuples can be constructed by '+' operator and the like.

Let us once again operating coral tuples, we want the items 'blue coral' replaced 'black coral'. If you try to operate as the list of actions as tuples:

file

It will complain.

This is because the value of the tuple can not be changed.

If you construct a tuple finished, then you really need is found in the form of a list, we can be converted.

To convert a tuple into a list, we can use the list () function.

file

We'll now converted into a tuple with [] wrapped in a list.

Similarly, you can use the tuple () function to convert a list of tuples.

You can "how to convert data in Python3" to learn more about them.

6, summed up

Tuple is an ordered data type, it can not be modified, because it runs faster than a listing, it will be using code tuple certain optimization. When you and others to work together to write code that uses tuple type means that you do not own the data sequence is modified.

Published 38 original articles · won praise 1 · views 2187

Guess you like

Origin blog.csdn.net/wulishinian/article/details/104916068