A. List
1. Create an instance:
A = [All-in-a = List ()
2. The main supported operations and their time complexity are as follows:
3. Other
The list in Python is actually stored in memory in the form of distributed storage, compared to vectors in the STL, which allocates a certain amount of memory beforehand, and when not enough, applies a new larger piece of memory to copy the original data. The deletion is similar to the reverse case, so if the number of stored elements often changes, use deque instead of better. Deque is a doubly linked list and does not go to the copy element.
We can also see that the complexity of the x in S is O (n), so it would be better to use set when creating the data store if it was only necessary to confirm the in condition, and the average complexity was O (1).
Three. Dictionaries
1. Usage examples:
1 a = {}2 b = dict ()3 c = {1:2, 2:3}
2. The main supported operations and their time complexity are as follows:
3. Other:
In Python2, the keys () method of the dictionary and the values () method return directly to the list, and in Python3, the types of Dictkey and Dictvalue are returned. They can iterate, but cannot do slicing operations, etc., and need to be converted to list or tuple.
Dictionaries are not needed, and the newly added elements may not be placed behind them. Need to implement an ordered dictionary, you can use Ordereddict.
Four. Tuples
1. Using the example
1 A = (1,)2 b = Tuple (1)
2. Supported operations
Tuples support the operation of lists other than additions and deletions, which are very similar.
3. Other
A tuple is like an immutable list, cannot add elements to it, and cannot change the value inside an element. But in fact, if the elements inside the tuple are mutable, they can also be changed:
It is also important to note that the parentheses are not represented as tuples, and if a tuple is initialized with only 1 elements, it must be followed by a comma after the 1 elements, otherwise it is not a tuple (as I wrote in the example code).
Five. For loop
The for is used to iterate over an object that is iterated, each time it gets one of the contents.
1 A = [1, 3, 4, 5]2 for in A:3 print i
Six. Enumerate
Examples of Use:
1 forI,jinchEnumerate (('a','b','C')):2 Printi,j3 4 forI,jinchEnumerate ({'a': 1,'b': 2}):5 Printi,j6 7 forI,jinchEnumerate ('ABC'):8 PrintI,j
The function is to add an ordinal to an iterative object, note that if used on dict, the element that is iterated out of the sequence number is dict key, not including value.
Seven. Range and xrange
Examples of Use:
1 Range (1,10,2)2 range (10,1,-1)3 range (len (lstname))
The function is based on the starting, terminating, and step three parameters, generating a number within a certain range. The difference between range and xrange is that the former generates the entire list directly, and the latter is an iterator that returns one at a time during the iteration.
In Python3, however, range and xrange have been merged into range, but the actual effect is the same as the xrange in 2.
Python Basic finishing Notes (ii)