Python Quick Check Notes (2) Built-in Type

xiaoxiao2021-03-06  64

Python's built-in type is extremely powerful, habitual, has great promotion of improving programming efficiency. Unfortunately, C is not so cheap.

Third, the built-in type 1, list: list (ie dynamic array, C standard library VECTOR, but can contain different types of elements in one list) A = ["i", "you", "he", "she" ] # Elements can be any type. Shiple: Press to write to write, when the array processing starts with 0, with a negative target, the first element, -1's first element, the first element of LEN-1, List The length of the number of elements, the length of #LIST. The actual method is to call the __len __ (self) method of this object. Create a continuous list l = range (1,5) # 即 L = [1, 2, 3, 4] without the last element L = RANGE (1, 10, 2) # 即 L = [1, 3, 5, 7, 9] List L.Append (var) # Add Element L. Insert (Index, Var) L.POP (VAR) # Returns the last element and removes L.Remove (var) from LIST # Remove the first time the element L.count (var) # This element appears in the list of L.index (var) #, the location of this element, is not allowed to throw the iptend (list) # appended List , Ie merged List to L on L.Sort () # Sort L.Reverse () # 倒 序 t t::: , *, Keyword Del A [1:] # 片段 操作,, for sub-list [ 1, 2] [3, 4] # [1, 2, 3, 4]. With extend () [2] * 4 # is [2, 2, 2, 2] DEL L [1] # Delete Element DEL L [1: 3] # 指 的 的 元 的 的 的 的= L # L1 is the alias of L, which is the same as the pointer address, and the L1 is operated. The function parameter is the L1 = L [:] # l1 is the clone of L, which is another copy. List comprehension [ for k in l if ] 2, Dictionary: Dictionary (ie the MAP of the C standard library) Dict = {'ob1': 'Computer', 'OB2': 'mouse', 'OB3' : 'printer'} Each element is Pair, including two parts of Key, Value.

Key is an Integer or String type, and Value is any type. The key is unique, the dictionary only recognizes the last key value. Dictionary's method D.Get (key, 0) # With DICT [Key], there is no one to return to the default value, 0. [] No, there is no need to throw D.has_key (key) # There is this button to return True, otherwise false d.keys () # 字 字 字 键 键 d D. Values ​​() D.Items () D.Update (DICT2) # Add Merge Dictionary D.PopItem () # get a pair and remove it from the dictionary. It is time to throw a D. CLEAR () # 清 清 Dictionary, Del Dict D.copy () # copy Dictionary D.cmp (DICT1, DICT2) # 比 比 dictionary, (priority is elementary, key size, key value Size) # The first big return 1, the small return -1, the same return 0 Dictionary copy Dict1 = DICT # alias DICT2 = DICT.COPY () # Clone, another copy.

3, TUPLE: Tuan group (ie, a constant array) TUPLE = ('A', 'B', 'C', 'D', 'E') can be extracted with the [],: operator extracting elements with LIST. It is not possible to modify the elements directly. 4, String: String (ie, the character list that cannot be modified) str = "Hello My Friend" string is a whole. It is impossible if you want to directly modify a part of the string. But we can read some part of the string. The extract of the sub-string STR [: 6] string contains judgment operators: IN, NOT IN "He" in Str "She" Not in String module, there are many ways, such as SUBSTRING, [START [, end]]) # 指 指 范围 查 子 子, return the index value, otherwise returns -1 s.rfind (Substring, [START [, END]) #Nound look for S.index (Substring, [Start [, End]] # 同,, just find the ValueError exception s.rindex (Substring, [START [END]]) # 同 上 上 上. (Substring, [START [, END]] # 回Find the number of substrs s.Lowercase () s.capitalize () # 首 大 大 写 S.Lower () # turn lowercase s.upper () # 转 大 写 S.swapcase () # 大小 换 S.Split ( Str, '') # Turn string to List, Split S.Join (List, '') # to connect LIST to String, with space connection to process strings of built-in functions LEN (STR) # string length CMP ("MY) Friend ", STR) # string comparison.

转载请注明原文地址:https://www.9cbs.com/read-119316.html

New Post(0)