Recently, because of work needs, using Python to develop the company's operations automation platform, so find a book and combined with the Official handbook, began the Python learning journey.
First, List
meaning: The list is expressed in brackets, separating a set of data by commas (which can be of different data types), such as the following declaration:
1 >>> language = [' Chinese ', ' 中文版 ', ' Japanese ']2 >>> contries = [' China ', ' Amercia ', ' England ', ' Japan ']3 4 >>> edward = [' Edward Gumby ',] #不同的数据类型5 >>> john = [' John Smith ', 50]6 >>> dat Abase = [Edward,john] #列表可以嵌套
"Action": Access, insert, delete, max minimum and length, etc.
1. Access: You can iterate through the list by index or Shard, the index can access the value of a location, the Shard can access a range of values flexibly, as follows:
1 >>> language = [' Chinese ', ' 中文版 ', ' Japanese '] 2 # index Access 3 >>> language[0] 4 ' Chinese ' 5 6 # shard Access 7 # The first index element is included in the Shard, the second index is not included in the Shard 8 # You can set the step size of the Shard 9 >>> language[:: ]10 [' Chinese ', ' 中文版 ', ' Japanese ']11 >>> language[0:3]12 [' Chinese ', ' 中文版 ', ' Japanese ']13 >>> language[0:2]14 [' Chinese ', ' 中文版 ']15 >>> language[1:2]16 [' 中文版 ']17 # Set the step size to 218 >>> num = [1,2,3,4,5,6,7,8,9]19 >>> num[0:9:2 ]20 [1, 3, 5, 7, 9]
>>> num = [1, 2, 3]
>>> max (num)
3
>>> min (num)
1
>>> len (num)
3
2, modify, insert and delete operations, it appears that the list can be modified.
1 #修改列表某个元素: Index assignment 2 >>> num = [] 3 >>> num[0]=5 #必须为存在的位置索引赋值, otherwise error 4 >>> num 5 [5, 2, 3 ] 6 7 #修改列表某段范围的值: Shard Assignment 8 >>> num[2:]=[6,7,9] #分片赋值元素个数可以不等长 9 >>> NUM10 [5, 2, 6, 7, 9]11 #删除某个元素 >>> del num[4]14 >>> num15 [5, 2, 6, 7]
"Method" for the list, Python has built in a number of methods for operation, mainly used in the following:
1 #append方法: Used to add elements at the end of the list, the method directly modifies the original list and returns a new list after the modification 2 >>> str = [' A ', ' B ', ' C ', ' d '] 3 >>> str.append (' E ') 4 ;>> STR 5 [' A ', ' B ', ' C ', ' d ', ' E '] 6 7 #count方法: Count the number of occurrences of an element in the list 8 >>> str = [' A ', ' B ', ' C ', ' d '] 9 >>& Gt Str.count (' a ') 111 #extend方法: List.extend (L), l refers to the list of objects #用另一个列表扩展一个列表, equivalent to two list connections, #但是又不同于连接操作, Because the Extend method modifies the original list directly and returns, #连接操作不影响原有的列表值16 >>> str1 = [' Hello, ']17 >>> str2 = [' World ']18 >>> St R1.extend (str2) >>> str120 [' Hello, ', ' World ']21 >>> str3 = [' A ']23 >>> STR4 = [' 5 ']24 ;>> STR3 = STR3+STR4 #效率没有extend高25 >>> str326 [' A ', ' 5 ']27 #index方法: Returns the first index position of a match >>> Knight = [' we ', ' you ', ' we ', ' me ', ' he ']30 >>> knight.index (' we ') 032 #insert方法: List.insert (i, X) insert x to the I position > >> Knight.insert (1, ' she ') >>> knight36 [' we ', ' she ', ' you ', ' we ', ' me ', ' he ']37 #remove方法: List.remove (x) #删除列表中为X的第一个出现元素40 >>> Knight = [' We', ' She ', ' you ', ' we ', ' me ', ' he ']41 >>> knight.remove (' we ') knight43 [' She ', ' I ', ' we ', ' Me ', ' He ']44 #reverse方法: Store elements in reverse chronological >>> str = [' A ', ' B ']47 >>> str.reverse () ~ str49 [' B ', ' a ']5 0 #sort方法: List.sort (Key=none, Reverse=false) #直接改变原列表顺序, not to change the copy, for such a requirement such as #仅仅对副本进行排序, do not change the original list can not be directly used Sort54 >> > num = [1,3,2,4]55 >>> num.sort () num57 [1, 2, 3, 4]58 #pop方法: List.pop ([i]) #删除指定索引位置的元素值 and returns the value of >>> num = [1, 2, 3, 4]62 >>> Num.pop (3) 63 4
"Common examples": simulating implementation of stack operations and queue operations
Stack: Last in, first out
1 >>> stack = [3, 4, 5] 2 >>> stack.append (6) 3 >>> stack.append (7) 4 >>> stack 5 [3, 4, 5, 6, 7] 6 >>> Stack.pop () 7 7 8 >>> Stack 9 [3, 4, 5, 6]10 >>> stack.pop () 612 >>&G T Stack.pop () 514 >>> stack15 [3, 4]
Queue: FIFO
1 >>> from collections import deque 2 >>> queue = Deque (["Eric", "John", "Michael"]) 3 >>> Queu E.append ("Terry") # Terry arrives 4 >>> queue.append ("Graham") # Graham arrives 5 >>> Queue.popleft () # The first to arrive now leaves 6 ' Eric ' 7 >>> queue.popleft () # The second to arrive no W leaves 8 ' John ' 9 >>> queue # Remaining queue in order of Arrival10 deque ([' Michael ', ' Terry ', ' Graham '])
Chapter One important data structures in Python (i.)