Create List sample_list = [' A ', 1, (' A ', ' B ')] Python list Operation sample_list = [' A ', ' B ', 0,1,3] get one of the values in the list Value_start = Sample_list[0]end_ Value = Sample_list[-1] Deletes the first value of a list del sample_list[0] Inserts a value in the list sample_list[0:0] = [' sample value '] Gets the length of the list list_length = Len (sample_list) List traversal for element in Sample_list:print ' element ' Python list advanced operations/techniques produce a numeric increment list num_inc_list = range (#w) Ill return a list [0,1,2,..., 29] Initializes the list with a fixed value initial_value = 0list_length = 5sample_list = [Initial_value for i in range (1 0)]sample_list = [initial_value]*list_length# sample_list ==[0,0,0,0,0] attached: Python built-in type 1, list: lists (i.e. dynamic arrays, vectors of the C + + standard library, But can contain different types of elements in a list) a = ["I", "You", "he", "she"] # elements can be any type. Subscript: Read and write according to subscript, as array processing starting at 0, negative subscript using 0 first element, 1 last element,-len first element, len-1 the last element the number of elements of the list Len (list) #list的长度. The actual method is the __len__ (self) method that called the object. Create a continuous LISTL = range (1,5) #即 l=[1,2,3,4], without the last element L = range (1, 2) #即 l=[1, 3, 5, 7, 9] List method L.append (Var) #追加元素L . Insert (Index,var) L.pop (Var) #返回最后一个元素 and remove the L.remove (Var) from the list #删除第The element L.count (Var) #该元素在列表中出现的个数L at one time. Index (VAR) #该元素的位置, none throws the exception L.extend (list) #追加list, that is, merge list to L L.sort () #排序L. Reverse () #倒序list operator:, +,*, keyword dela[1:] #片段操作符 for extracting the sub list [1,2]+[3,4] #为 [1,2,3,4]. Same as Extend () [2]*4 #为 [2,2,2,2]del l[1] #删除指定下标的元素del l[1:3] #删除指定下标范围的元素list的复制L1 = L #L1为L的别名, in C is the same pointer address, to L1 Operation is done on L. The function parameter is the L1 = l[:] #L1为L的克隆, that is, another copy. List comprehension [<expr1> for k in L if <expr2>] 2, Dictionary: Dictionary (that is, map of the C + + standard library) dict = {' OB 1 ': ' Computer ', ' ob2 ': ' Mouse ', ' ob3 ': ' printer '} Each element is a pair, containing the key, value two parts. Key is an integer or string type and value is any type. The key is unique, and the dictionary only recognize the last assigned key value. Dictionary method D.get (key, 0) #同dict [key], more than one does not return the default value, 0. [] No, throw exception D.has_key (key) #有该键返回TRUE, otherwise Falsed.keys () #返回字典键的列表D. VALUES () #以列表的形式返回字典中的值, the list of returned values can contain duplicates Element D.items () #将所有的字典项以列表方式返回, each of these lists comes from (the key, the value), but the items are returned with no special Order d.update (DICT2) #增加合并字典D. Popitem () #得到一个pair and remove it from the dictionary. Empty then throw exception d.clear () #清空字典, with deL dictd.copy () #拷贝字典D. CMP (DICT1,DICT2) #比较字典, (priority is number of elements, key size, key value size) #第一个大返回1, small return-1, return 0 Dictionary copy dict1 = dict #别名dict2 =dict.copy () #克隆, or another copy. 3, tuple: tuples (that is, constant array) tuple = (' A ', ' B ', ' C ', ' d ', ' e ') can be used to extract elements from the list's []: operator. Is that you cannot modify elements directly. 4, String: character (that is, cannot be modified list) str = "Hello My Friend" string is a whole. If you want to modify a part of a string directly, it is not possible. But we can read a certain part of the string. Substring extraction Str[:6] string contains the judgment operator: In,not in "He" in str "she" is not the in STR string module, also provides a number of methods, such as S.find (substring, [start [, end]]) # can refer to Range lookup substring, return index value, otherwise return -1s.rfind (Substring,[start [, end]]) #反向查找S. Index (Substring,[start [, end]]) #同find, Just can't find the resulting ValueError exception s.rindex (Substring,[start [, end]]) #同上反向查找S. Count (Substring,[start [, end]]) #返回找到子串的个数 S.lowercase () s.capitalize () #首字母大写S. Lower () #转小写S. Upper () #转大写S. Swapcase () #大小写互换 S.split ( STR, ') #将string转list, S.join (list, ') #将list转string with a space to concatenate the built-in function len (str) #串长度cmp that handles the string ("My Frien D ", str) #字符串比较. The first big one, returns 1max (' abcxyz ') #寻找The largest character in the string min (' abcxyz ') #寻找字符串中最小的字符 string conversion of float (str) #变成浮点数, float ("1e-1") result is 0.1int (str) #变成整型, the Int ("12") result is 12int (str,base) #变成base进制整型数, the result of Int ("11", 2) is 2long (str) #变成长整型, Long (str,base) #变成base进制长整型, The formatting of the string (note its escape character, mostly as in C, slightly) Str_format% (parameter list) #参数列表是以tuple的形式定义的, that is, cannot be changed >>>print ""%s ' s height is%dcm "% (" My brother "," #结果显示为 my brother ' s height is 180cm .......... Mutual transformation of list and tuple tuple (LS) list (LS)
Python basic operations