27.Python list (list), tuple (tuple), Dictionary (dict) and the set (set) Comments

This chapter describes the Python built-in four commonly used data structures: list (list), tuple (tuple), Dictionary (dict) as well as a collection (set).

Once these four data structures can be used to store multiple data items, which is very important for the program, as the program only requires the use of a single variable to hold the data, also you need to use a variety of data structures to store large amounts of data, and lists, tuples, dictionaries and collections can meet the needs of a large number of saved data.

List (list) and tuple (tuple) is quite similar, they are in order to save elements, each element has its own index, so the lists and tuples can be accessed via an index element. The difference is that tuples can not be modified, but the list is modified.

Dictionary (dict) and set (set) Similarly, their stored data are unordered where the dictionary is  key-value stored in the form of data.

 

The so-called sequence, refers to a plurality of values can be stored in contiguous memory space, these values arranged in a certain order, which can be accessed by a number (called an index) for each position where the value.

For more visual understanding of the sequence, it can be seen as an inn, each room in the store just as a sequence of stored data, a memory space, each room unique room number is equivalent to the index value. In other words, by room number (index) we can find the hotel (sequence) in each room (memory).

In  Python  , the sequence type including strings, lists, tuples, sets and dictionaries, which support the following general sequence of operation, but is rather special, dictionaries set and does not support indexing, slicing, addition and multiplication operations .

String is also a common sequence, it can also be accessed directly through the characters in the string index.

Sequence index

Sequence, each element has its own number (index). Elements from the starting index value incremented from 0, as shown in FIG.



FIG schematic sequence index value 1


In addition, Python supports index value is negative, such an index count from right to left, in other words, starts counting from the last element, starting from the index value of -1, as shown in FIG.



FIG 2 a schematic view of a negative index

Note that, in a negative value as the index value of each element in a column sequence used, start from -1, instead of from zero.

Whether using positive index value, or negative index value, you can access any element in the sequence. String, for example, access the "C language Chinese network," the first element and the last element, you can use the following code:

  1. str = "C language Chinese net"
  2. print(str[0],"==",str[-6])
  3. print(str[5],"==",str[-1])

The output is:

C == C
network == Network

Sequence slice

The slicing operation is another method to access elements in the sequence, it can access a range of elements, by a dicing operation, a new sequence may be generated.

Slicing operation sequence to achieve the following syntax:

sname[start : end : step]

Wherein the meaning of the various parameters are:

  • sname: indicates the name of the sequence;
  • start: start index indicates the position of the slice (including the location), this parameter may not be specified, the default is 0, that is, from the beginning of the sequence slicing;
  • end: end slice index indicates a position (this position is not included), if not specified, the default is the length of the sequence;
  • step: shows the slicing process, every few memory locations (including the current position) to take time element, i.e., if the step value is greater than 1, then when the slice is performed to sequence elements, may "jump" is taken element . If the set value of the step is omitted, then the last colon can be omitted.


For example, the string "C language Chinese network" slicing:

  1. str = "C language Chinese net"
  2. # Take interval between index [0,2] (not including the character at index 2) of the string
  3. print(str[:2])
  4. # 1 character takes a separator character, the entire section is a string
  5. print(str[::2])
  6. # Take the entire string, this time in [] simply by a colon
  7. print(str[:])

Operating results as follows:

C language
C Were made by
C language Chinese network

Sequences are added

Python, support the same two types of sequences using the "+" operator make the addition operation, it will be connected to two sequences, but does not remove duplicate elements.

The term "same type" refers to the "+" operator or the flanking sequences are sequence type, either both tuple type, either both strings.

For example, we have achieved in the previous section with the "+" operator connected to two (or even more) strings, as follows:

  1. str="c.biancheng.net"
  2. Print ( "C language" + "Chinese Network:" + str )

The output is:

C language Chinese network: c.biancheng.net

Multiplying the sequence

Python, a new sequence by multiplying the number n generates a sequence, the contents of the original sequence is repeated n times result. E.g:

  1. str = "C language Chinese net"
  2. print(str*3)

The output is:

'C network C language Chinese language Chinese language Chinese net net C'


Is special, when performing multiplication list type, may also be implemented function initializes the specified length of the list. For example, the following code would create a list of length 5, each element of the list is None, represents nothing.

  1. Created with [], will explain in detail the subsequent list # list
  2. list = [None]*5
  3. print(list)

The output is:

[None, None, None, None, None]

Check element is included in the sequence

Python can be used in a keyword to check whether an element is a member of the sequence, its syntax is:

value in sequence

Wherein, value represents an element to be inspected, sequence represented by the specified sequence.

For example, check character 'c' is included in the character string "c.biancheng.net" may be performed following code:

  1. str="c.biancheng.net"
  2. print('c'in str)

Operating results as follows:

True


And in the same keyword usage, but function just the opposite, there are not in a keyword, use it to check whether an element not contained in the specified sequence, for example:

  1. str="c.biancheng.net"
  2. print('c' not in str)

The output is:

False

And serial correlation of built-in functions

Python provides several built-in functions (Table 3), may be used to implement some of the operations associated with the common sequence.

Table 3 built-in functions related sequences
function Features
Only () Calculating the length of the sequence, i.e., it returns the number of elements contained in the sequence.
max() To find the largest element in the sequence. Note that, when using the sequence of sum () function, are digital summation must be operated, a character or character string can not otherwise function throws an exception, because the interpreter can not determine the connecting operation is to be done (the + operator you can connect two sequences), or do add and operation.
(I) Find the smallest element in the sequence.
list() The sequence into a list.
str() Sequence into a string.
sum() And computing elements.
sorted() Sort elements.
reversed() Reverse sequence elements.
enumerate() The sequence is a combination of the sequence index, is used in a for loop.


We are here to give a few examples:

  1. str="c.biancheng.net"
  2. # Find the maximum character
  3. print(max(str))
  4. # Find the smallest character
  5. print ( min (str ))
  6. # Of string Sorts the elements
  7. print(sorted(str))

The output is:

t
.
['.', '.', 'a', 'b', 'c', 'c', 'e', 'e', 'g', 'h', 'i', 'n', 'n', 'n', 't']

Guess you like

Origin www.cnblogs.com/youqc/p/12068202.html