1 #!/user/bin/env python 2 # -*- coding:utf-8 -*- 3 4 """ 5 Container: 6 List: 7 Establish 8 Content Access 9 Built-In Function 10 Add 11 append() 12 extend(iterable) 13 insert(index, object) 14 15 Delete 16 remove(object) 17 pop([index]) 18 Remove and return item at index (default last). 19 clear() 20 del 21 numbers = [1, 2, 3, 5, 6, 7] 22 del numbers[3] 23 # >>> [1, 2, 3, 6. 7] 24 25 del numbers 26 NameError: name 'numbers' is not defined 27 28 Modify and Find 29 numbers = [1, 2, 3, 4, 5] 30 numbers[numbers.index(3)] = 4 31 # >>> [1, 2, 4, 4, 5] 32 33 else 34 string = numbers.copy() 35 Shallow Copy : Return a new address, that the contents are the same as before 36 Copy the parent object, not the child object inside the object 37 # >>> numbers[1, 2, 3, 4, 5] 38 # >>> string[1, 2,, 3, 4, 5] 39 # >>> id(numbers) 3891784 40 # >>> id(string) 3891848 41 42 numbers = [1, 2, 3, 4, 5] 43 new_numbers = numbers[:3] 44 # >>> id(numbers) 31131720 45 # >>> id(new_numbers) 31131784 46 47 48 numbers.count() 49 numbers = [1, 1, 3, 5, 5, 5, 5] 50 numbers.count(5) 51 # >>> 4 52 53 list-Supported operators : 54 + * [] in is 55 56 Operational-related system Built-in Function : 57 numbers = [1, 3, 5, 7, 9] 58 # >>> sum(numbers) 25 59 # >>> min(numbers) 1 60 # >>> max(numbers) 9 61 # >>> sorted(numbers) [1, 3, 5, 7, 9] 62 # >>> sorted(numbers, reverse=True) [9, 7, 5, 3, 1] 63 64 Tuple: 65 Establish 66 numbers = (1, ) 67 Content Access 68 numbers = (1, 3, 5, 7, 9) 69 # >>> numbers[2] 70 # >>> 5 71 # >>> numbers[-2] 72 # >>> 7 73 # >>> numbers[:3] 74 # >>> (1, 3, 5) 75 Built-In Function 76 Do not support (add, delete, modify) : 77 tuple->list->tuple 78 find : 79 tuple.index() 80 tuple.count() 81 82 sorted(numbers) # >>> [1, 3, 5, 7, 9] 83 # >>> type(numbers) 84 # >>> <class 'list'> 85 86 list-Supported operators : 87 + * [] in is 88 89 Set : Disorder and without repetition 90 Establish 91 numbers = set() 92 numbers = {1, 2, 3} 93 Content Access 94 Built-In Function 95 add : 96 numbers.add(object) 97 numbers.update(Iterable) 98 delete 99 numbers.remove(object) 100 numbers.discard(object) 101 numbers.pop() 102 numbers.clear() 103 Do not support (modify and find) 104 else : 105 for index, value in enumerate(numbers): 106 print(index, value) 107 # >>> 0 1 108 # >>> 1 2 109 # >>> 2 3 110 111 list-Supported operators : 112 in is 113 set operation : & numbers.intersection(object) 114 numbers.intersection_update(object) 115 | numbers.union(object) 116 - numbers.difference(object) 117 ^ numbers.symmetric_different(object) 118 numbers.symmetric_different_update(object) 119 120 Dict 121 Establish 122 dictionary = {} 123 dictionary = dict() 124 Content Access 125 dictionary = {"GeBiCunGaHa": 21, "YeYuanXinZhiZhu": 5} 126 dictionary["GeBiCunGaHa"] 127 # >>> 21 128 if key does not exist, Throw out KeyError 129 dictionary.get(GeBiCunGaHa") 130 # >>> 21 131 dictionary.get("TieBiATongMu") 132 # >>> None 133 dictionary.get("TieBiATongMu", 10) 134 # >>> 10 135 Built-In Function 136 add 137 dictionary["TieBiATongMu"] = 18 138 delete 139 dictionary.pop("TieBiATongMu") Remove "TieBiATongMu" from maps[0] and return its value. 140 dictionary.popitem() remove and return some (key, value) pair as a 2-tuple 141 dictionary.clear() 142 del dictionary["TieBiATongMu"] 143 del dictionary 144 modify 145 dictionary["GeBiCunGaHa"] = 25 146 Do not support (find) 147 else : 148 dict.fromkeys(["one", "two", "three"], 100) 149 # >>> {'one': 100, 'two': 100, 'three': 100} 150 151 dictionary.keys() 152 # >>> dict_keys(['GeBiCunGaHa', 'YeYuanXinZhiZhu']) 153 dictionary.values() 154 # >>> dict_values([21, 5]) 155 dictionary.items() 156 # >>> dict_items([('GeBiCunGaHa', 21), ('YeYuanXinZhiZhu', 5)]) 157 dict-Supported operators : 158 in 159 for element in dictionary: 160 print(element) 161 # >>> "GeBiCunGaHa" 162 # >>> "YeYuanXinZhiZhu" 163 is 164 165 """