Python list has a lot of knowledge to learn. Next let's take a look at the relevant code. Only continuous learning can we better write the relevant code. The Python list (list) is similar to the variable array ArrayList in C # and is used for sequential storage structure.
The list is represented by the symbol [], and the elements in the middle can be any type including the List itself to implement multi-dimensional arrays). elements are separated by commas. When values or values are assigned, they can be indexed by location like the C array:
- #-*-Coding: UTF-8 -*-
- Array = [1, 2, 3]
- Print array [0]
- # Output 1
- Array [0] = 'A'
- Print array
- # Output ['A', 2, 3]
From the code above, you may find an interesting thing: different types of data can be mixed in the Python list, such as ['A', 2, 3, however, I do not recommend that you do this. I think it is no good, although it may be more convenient in some cases ).
- How to convert a Python program to an EXE file
- How to bind a C ++ program to Python
- Python display UTF-8 Chinese Text specific operation method
- Main features of Python objects
- Summary of Python experience
In addition, we can see that the list is a variable sequence, that is, we can change the object (value) stored at a location in the "in-situ" list ).
In C #, most operations supported by ArrayList, such as append, insert, delete, clear, sort, reverse, and count, are also supported, at the same time, list also supports "slicing. A slice refers to a part of the extraction sequence in the form of list [start: end: step]. The extraction rule is: start, add step each time until end. The default step is 1. When start is not given, it starts from the first element of list by default. When end =-1, it indicates the last element of list, and so on. For some simple examples, see the following code:
- #-*-Coding: UTF-8 -*-
- Test = ['Never ', 1, 2, 'yes', 1, 'No', 'maybe']
- Print test [0: 3] # includes test [0], excluding test [3]
- Print test [] # includes test [0], excluding test [6], and the step size is 2
- Print test [:-1] # includes the start, excluding the last
- Print test [-3:] # extract the last three
String, list, And tuples support slicing. This is very convenient and you should learn to use it skillfully.
Finally, list is the most basic data structure in the Python list. You can use it as a linked list, stack, or queue, which is highly efficient. Python does not have a fixed-length array. If you really care about the performance, you can import the array module to create a C-style array, which is highly efficient and will not be detailed here.
Python list tuples (tuple)
Tuples are very similar to lists. They are sequences enclosed by () rather. Tuples are faster than lists, but tuples are an immutable sequence, that is, they cannot change their values in the same way as str. In addition, other attributes are basically the same as those in the list.
The method for defining tuples is similar to that for the list. However, when defining tuples that only contain one element, add a comma behind them. See the following statement differences:
- #-*-Coding: UTF-8 -*-
- Test = [0] # The list can be defined as follows
- Print type (test) # output <type 'LIST'>
- Test = [0,] # can also be defined as this
- Print type (test) # output <type 'LIST'>
- Test = (0,) # tuples can be defined as follows
- Print type (test) # output <type 'tuple'>
- Test = (0) # but cannot be defined like this. Python considers it a bracket expression print type (test) # output <type 'int'>
- Test = 0, # brackets can also be omitted, but note that it is different from the comma expression in C.
- Print type (test) # output <type 'tuple'>
Using this feature of tuples, You can simplify the initialization process of Python variables, such:
- x,y,z=1,2,3
You can also easily exchange data. For example:
- a = 1
- b = 2
- a,bb = b,a
The preceding statements are widely used in the Python list for variable exchange and function value passing. Therefore, the Python interpreter is constantly optimizing them and now has a high efficiency. Therefore, the above Code is faster than the conventional statement tmp = a; a = B; B = tmp in Versions later than Python 2.5.