1 List1.1 List use List functions: append, extend, insert, remove, pop, index, count, sort, reverse >>> a [3, 1, 2] >>> B [4, 7, 6, 10] >>>. append (5) >>> a [3, 1, 2, 5] >>>. extend (B) >>> a [3, 1, 2, 5, 4, 7, 6, 10] >>>. insert () >>> a [9, 3, 1, 2, 5, 4, 7, 6, 10] >>>. insert () >>> a [9, 8, 3, 1, 2, 5, 4, 7, 6, 10] >>>. insert (5, 1) >>> a [9, 8, 3, 1, 2, 1, 5, 4, 7, 6, 10] >>>. remove (1) >>> a [9, 8, 3, 2, 1, 5, 4, 7, 6, 1 0] >>>. pop (0) 9 >>> a [8, 3, 2, 1, 5, 4, 7, 6, 10] >>>. pop () 10 >>> a [8, 3, 2, 1, 5, 4, 7, 6] >>>. index (1) 3 >>>. insert () >>> a [8, 3, 6, 2, 1, 5, 4, 7, 6] >>>. count (3) 1>. count (6) 2>. sort () >>> a [1, 2, 3, 4, 5, 6, 6, 7, 8] >>>. reverse () >>> a [8, 7, 6, 6, 5, 4, 3, 2, 1] >>> 1.2 treats List as a Stack) using the append mentioned above, the pop function can easily use the List as a stack. Stack features advanced and later features. >>> Stack = [5, 2, 8] >>> stack. append (9) >>> stack [5, 2, 8, 9] >>> stack. append (1) >>> stack [5, 2, 8, 9, 1] >>> stack. pop () 1 >>> stack [5, 2, 8, 9] >>> stack. pop () 9 >>> stack [5, 2, 8] 1.3 treats the List as a queue List and is easy to use as a queue. The queue features FIFO. However, the efficiency is very low. This is because when an element is deleted in the queue header, all elements following it must be moved left. You can use collections. deque to implement queues. It is specially used for queues, and its design ensures the efficiency of queuing and queuing. >>> From collections import deque >>> queue = deque (["Linux", "Unix", "Windows"]) >>> queuedeque (['linux ', 'unix ', 'windows']) >>> queue. append ("Mac") >>> queuedeque (['linux ', 'unix', 'windows', 'mac']) >>> queue. popleft () 'linux '>>> queuedeque (['unix', 'windows', 'mac']) 1.4 functional programming tool for List, there are three functions that are very useful: filter, map, reducefilter (function, sequence) selected from the sequence to enable the function to return real elements >>> def f (x ): return x % 2! = 0 and x % 3! = 0...> filter (f, range (1, 20) [1, 5, 7, 11, 13, 17, 19] map (function, sequence) function is applied to every element in sequence and the corresponding value is returned >>> def f (x): return x * 2 + 1... >>> map (f, range (1, 10) [3, 5, 7, 9, 11, 13, 15, 17, 19] If the function has two parameters, two sequences are required >>> def f (x, y): return x + y... >>> map (f, [1, 5, 2], [6, 3, 9]) [7, 8, 11] reduce (function, sequence) function of binary function is applied to the first two elements of sequence, and then the result is used together with the next element as the function parameter >>> def f (x, y ): return x + y... >>> re Duce (f, [1, 3, 4]) 10 >>> def f (x, y): return x * y... >>>> reduce (f, [241.5,]) List ComprehensionsList Comprehensions is a method for creating a List. for example, >>> squares = [x ** 2 for x in [, 3, 7] >>> squares [16, 9, 49] >>> [(x, y) for x in [1, 2, 3] for y in [3, 2, 4] if x! = Y] [(1, 3), (1, 2), (1, 4), (2, 3), (2, 4), (3, 2 ), (3, 4)] This method sometimes avoids writing a lot of code and is clearly readable. It is similar to defining a set in mathematics, as is haskell. In addition, this product can also be nested with play >>> matrix = [... [1, 2, 3, 4],... [5, 6, 7],... [8, 9, 10, 11, 12]...]> matrix [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10, 11, 12] >>> [[row [I] for row in matrix] for I in range (3)] [[1, 5, 8], [2, 6, 9], [3, 7, 10] Of course, this only reflects the features of List Comprehensions. It is more convenient to play with it: >>> zip (* matrix) [(1, 5, 8), (2, 6, 9), (3, 7, 10)] 2 del Statement> a =, 728] >>> a [5, 2, 67, 1, 72, 1,728] >>> del a [0] >>> a [2, 6 7, 1, 72, 1,728] >>> del a [] >>> a [2, 72, 1,728] >>>> del a [:] >>> a [] >>> del a >>> aTraceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'A' is not defined3 Tuples and the sequence that represents the sequence. There is also a standard data structure, Tuples. The elements in Tuple cannot be modified. Although Tuple and List seem very similar, they are used in different scenarios and will be introduced later. >>> T = "linux", "unix", "winx" >>> t ('linux ', 'unix', 'winx') >>> p = ("ubuntu ", "arch", "freebsd") >>> p ('ubuntu ', 'arch', 'freebsd ') >>> t [0] 'linux '>>> t [0] = "* nix" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>>> s = [1, 2, 3] >>> s [1, 2, 3] >>> s [0] = 9 >>> s [9, 2, 3] Tuple has two concepts: packing and unpacking. t = "linux ", "Unix", "freebsd" unpacking is: >>> x, y, z = t> x 'linux '> y 'unix'> z 'freebsd '4 Setspython also has a data structure, Sets. It is used to store unordered sets of non-repeating elements, and is often used for member testing to eliminate repeated items. Sets support intersection, difference, and other operations in the mathematical set. >>> OS = ['linux ', 'windows7', 'freebsd'] >>> oset = set (OS) >>> osetset (['windows7', 'freebsd ', 'linux ']) >>> 'linux' in osetTrue >>> 'unix 'in osetFalse >>> a = set ('sdkf; ahdf') >>> B = set ('safj13325er ')> a-B # where a is not in bset (['h', 'k', 'D','; ']) >>> a | B # In a or in bset (['R', 'A', 'E', 'D', 'F', 'h', 'k ', 'J', '3', '1', 's', '2', '5', ';'])> a & B # In a and in bset (['A', 's', 'F'])> a ^ B # In a or B and not at the same time A, bset (['E', 'D', 'h', 'k', 'J', '1', '3', '2', '5 ', ';', 'R']) the Set also supports the comprehensions feature >>> a = {x for x in 'abcde' if x not in 'abe '}>>> aset (['C ', 'D']) However, you must note that {} is not allowed when defining a set, but another useful data structure in set () 5 DictionariesPython is Dictionary. It consists of two parts: key: value. You can find the value through the key value. The key value is unique. >>> Tel = {'jack': 123, 'Lucy ': 456 }>>> tel ['Lucy'] 456 >>>> tel {'jack': 123, 'Lucy ': 456 >>>> tel ['lily'] = 789 >>>> tel {'lily': 789, 'jack': 123, 'Lucy ': 456 }>>> tel. keys () ['lily', 'jack', 'Lucy ']> 'lily' in telTrue uses a sequence that can be composed of (key, value) to directly construct a dictionary. you can also construct a dictionary using comprehension. >>> dict ([('linux ', 123), ('unix', 456)]) {'unix ': 456, 'linux ': 123 >>>> dict (linux = 826, unix = 910) {'unix ': 910, 'linux ': 826 >>>>{ x: x * 2 + 1 for x in (3,7, 1)} {1: 3, 3: 7, 7: 15} 6 loop technology when we want to traverse a sequence, we can use subscript, value, enumerate () to complete >>> for I, v in enumerate (['linux ', 'unix ', 'winx']):... print I, v... 0 linux1 unix2 winx when we want to traverse two or more sequences, we can use zip () to complete >>> ask = ['name', 'system ', 'editor'] >>> answer = ['Steve ', 'mac', 'ecs'] >>> for q, a in zip (ask, answer ):... print 'What is your {0 }? It is {1}. '. format (q, a)... What is your name? It is Steve. What is your system? It is Mac. What is your editor? It is emacs. if you want to traverse in reverse order, you can use reversed () to complete >>> for I in reversed (['winx', 'unix ', 'linux']):... print I... linuxunixwinx if you want to sort and traverse it, you can use sorted () to complete >>> for I in sorted (['unix ', 'linux', 'winx']) :... print I... linuxunixwinx to traverse a dictionary, you can use iteritems ()> system = {'unix ': 'second', 'linux': 'First ', 'winx ': 'last' }>>> for key, value in system. iteritems ():... print key, value... unix secondwinx las In tlinux first7, more conditional operations www.2cto.com for while and if are not only compared, but also more available operations. In and not in can be used to determine whether an element is in a sequence. is and not is can be used to determine whether two objects refer to the same object comparison operation and can be linked up, for example, a <B = c indicates that a <B and B = c have well-known Bool operations: and, or, not. They can be used together with comparison operations. Bool operations have a lower priority than comparison operations, among them, the not priority is the highest, or the lowest 8 Comparison sequence operation sequence objects can also be compared, they are compared in the lexicographical ordering order >>> (, 3) <, 4) True >>> (,) <(, 3) False >>> (, 3,-1) <(, 3) false >>> (1.0, 3) ==( 2.0, 3.0,) True