1. List:
>>> a=[123,333,333,1,145.5]>>>Print(A.count (333), A.count (1), A.count ('x'))#list.count (x) returns the number of occurrences of x in the list(2, 1, 0)>>> A.insert (2,-1)#List.insert (i,x) inserts an element x at the specified position I>>> A.append (333)#list.append (x) add X to the end of the list>>>a[123, 333,-1, 333, 1, 145.5, 333]>>> A.index (333)#list.index (x) returns the index of the first element in the list that has a value of x. If there are no matching elements, an error is returned1>>> A.remove (333)#list.remove (x) deletes the first element in the list with a value of x. If there is no such element, an error is returned>>>a[123,-1, 333, 1, 145.5, 333]>>> A.reverse ()#list.reverse () inverted list element>>>a[333, 145.5, 1, 333,-1, 123]>>> A.sort ()#List.sort () sort the list elements (not exactly how they are sorted, numbers are small to large)>>>a[-1, 1, 123, 145.5, 333, 333]#methods such as INSERT, remove, or sort modify the list without returning a value
View Code
2. Use the list as a stack:
Stack: Advanced back out
The list method allows the list to be used as a stack, using the Append () method to add an element to the top of the stack. A pop () method that does not specify an index can release an element from the top of the stack
>>> stack=[3,4,5]>>> stack.append (6)>>> stack.append (7)> >> stack[3, 4, 5, 6, 7]>>> stack.pop () # do not specify a location by default delete the last 7> >> stack.pop ()6>>> stack[3, 4, 5]
View Code
3. Use the list as a queue
Queue: FIFO
Start by understanding Python's common built-in modules: collections
1.namedtuple function: Use it to create a custom tuple of tuples object, it has tuples of tuple invariant, and can be referenced according to attributes, use is very convenient, note d don't write out haha
from Import namedtuple>>> point=namedtuple ('point', ['x' ,'y']) # represents the two-dimensional coordinates of a point >>> p=point >>> p.x1>>> p.y2
#类似的, a circle is represented by a coordinate and a radius, or it can be defined with Namedtuple
Circle=namedtuple (' Circle ', [' X ', ' y ', ' r '])
#总结语法: Namedtuple (' name ', [properties])
2.deque (dual-ended queue)
Purpose: To efficiently implement a two-way list of insert and delete operations, suitable for use in queues and stacks
Cause: When you use a list to store data, accessing the elements by index is very fast, but inserting and deleting elements is slow because the list is linear, and when the data is large, insertions and deletions are inefficient.
deque
In addition to implementing the and outside of the list append()
pop()
, support appendleft()
and popleft()
, in this way, can be very efficient to add or remove elements to the head
>>> fromCollectionsImportdeque>>> Q=deque (['a','b','C'])>>> Q.append ('x')>>> Q.appendleft ('y')>>>Qdeque (['y','a','b','C','x'])>>>Q.pop ()'x'>>>Q.popleft ()'y'>>>Qdeque (['a','b','C'])#Summary syntax: Define queue Q=deque (list)
#deque支持头部添加appendleft () and head Delete Popleft (), it is only efficient to have the list as a queue.
List Deduction formula:
>>> vec=[2,4,6]>>> [3*x forXinchVEC] [6, 12, 18]>>> [[X,x**2] forXinchVEC] [[2, 4], [4, 16], [6, 36]]>>> [3*x forXinchVecifX>3][12, 18]>>> [3*x forXinchVecifX<2][]>>> vec1=[2,4,6]>>> vec2=[4,3,-9]>>> [X*y forXinchVec1 forYinchVEC2] [8, 6,-18, 16, 12,-36, 24, 18, 54]>>> [Vec1[i]*vec2[i] forIinchRange (len (VEC1))] [8, 12, 54]>>> [Str (Round (355/113,i)) forIinchRange (1,6)]#round (x,i) returns the rounding value of the floating-point number x, preserving the decimal place is I['3.0','3.0','3.0','3.0','3.0']>>> [X.strip () forXinchVEC] Traceback (most recent): File"<stdin>", Line 1,inch<module>Attributeerror:'int'object has no attribute'Strip' #wrong is the type>>> vec3=['John','Jack','Tom']>>> [X.strip () forXinchVEC3] ['John','Jack','Tom']
View Code
Basic content of Python data structures