Python3 basic list instance parsing, python3list
Generally, any value in Python is an object, so any type (int, str, list ...) Is a class. The class must have its methods or attributes. It is obviously impossible to write down all the methods of so many classes. This article introduces two tips:
Dir (): a built-in function used to query all attributes of a class or object, such as >>> dir (list ).
Help (): a built-in function used to query specific instructions, such as >>> help (int ).
In the Basic Data Type of Python3 in the previous article, we first learned about the list and introduced that the list is the most frequently used data type in Python. This article will further explore the use of the list.
I. list method:
List. append (x)
Add an item at the end of the list, which is equivalent to a [len (a):] = [x].
List. extend (L)
Link the given list L to the current list, which is equivalent to a [len (a):] = L.
List. insert (I, x)
Insert an entry before the given position I, for example,. insert (0, x) is inserted in the header of the list, while. insert (len (a), x) is equivalent to. append (x ).
List. remove (x)
Remove the item whose first value is x from the list. If no value exists, an error is returned.
List. pop ([I])
Delete an item at a given position in the list and return it. If no index is specified, a. pop () is removed and the last item in the list is returned. (Square brackets indicate optional)
List. clear ()
Delete all items in the list, which is equivalent to del a [:].
List. index (x)
Returns the index of the first x item in the list. If no matching item exists, an error is generated.
List. count (x)
Returns the number of times x appears in the list.
List. sort ()
Sort the list in the local directory.
List. reverse ()
The list items are flipped in place.
List. copy ()
Returns a shortest copy of the list, which is equivalent to a [:].
Ii. List as stack
The List method makes it easy to use as a stack. We know that the stack feature is that the final incoming elements come out first (that is, the latter comes out first), the append () method is used to press the stack, and pop () with no specified index is used () method.
The sample code is as follows:
Stack = [] for x in range (1, 6): stack. append (x) # print ('push', x, end = '') print (stack) print ('now stack is ', stack) while len (stack)> 0: print ('pop', stack. pop (), end = '') # print (stack)
Iii. List as a queue
The list can also be used as a queue. The queue feature is that the first element to be added is obtained first (that is, first-in-first-out ). However, the efficiency of using a list as a queue is not high, because adding and popping elements from the end of the list is fast, insert or pop-up at the beginning of the List is relatively slow (because all elements have to move one position ).
To implement a queue, use the standard library collections. deque, which is designed to be added at both ends and popped up quickly.
The sample code is as follows:
From collections import dequequeue = deque () # create an empty queue for x in range (1, 6): queue. append (x) # print ('push', x, end = '') print (list (queue) print ('now queue is ', list (queue )) while len (queue)> 0: print ('pop', queue. popleft (), end = '') # print (list (queue ))
Iv. List Derivation
List derivation provides a simple way to create a list from a sequence. Generally, the program performs operations on each element of the sequence and uses the result as the element of the new list, or creates a subsequence Based on the specified conditions.
The structure of the list derivation formula is: in a square brackets, first an expression, then a for clause, and then zero or more for or if clauses. The returned result is a list generated from the for and if context after the expression.
The sample code is as follows:
Squares = [x ** 2 for x in range (10)] # deduced print (squares) # The output is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] pairs = [(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x! = Y] # deduced print (pairs) # The output is [(1, 3), (1, 4), (2, 3), (2, 1 ), (2, 4), (3, 1), (3, 4)]
5. List nesting
Python does not have the concept of a two-dimensional array, but we can achieve the same goal through list nesting.
mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
Similarly, we can use the derivation to generate a nested list:
Mat = [[, 3], [, 6], [, 9] new_mat = [[row [I] for row in mat] for I in, 2] # nested print (new_mat) # output [[1, 4, 7], [2, 5, 8], [3, 6, 9]
Appendix: del statement
The del statement can delete items in the list by specifying an index (rather than a value). It is different from the pop () method that returns a value. The del statement can also remove Slices from the list or clear the entire list:
Lst = [,] del lst [2] # delete a specified index entry print (lst) del lst [] # delete a slice print (lst) del lst [:] # Delete the entire list print (lst) del can also be used to delete the variable entity: del lst
If you reference lst after deleting the variable entity, an error occurs.
In python33, how does one perform operations between two different lists?
I am from python2.7.5, and I don't know if 3.3 is applicable! Try:
For python33, you can use the list and range functions to return a cumulative sum and list.
Is it similar to the Fibonacci series?
Def cumulative_sum (l): new_l = [] new_l.append (l [0]) for old in l [1:]: new_l.append (new_l [len (new_l)-1] + old) return new_l