Python list array common operations collection "Go"

Source: Internet
Author: User
Tags throw exception python list

The list in Python is similar to a mutable array (ArrayList) in C # for sequential storage structures. Create List sample_list = [' A ', 1, (' A ', ' B ')]python list Operation sample_list = [' A ', ' B ', 0,1,3] get a value 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/tricks produce a numeric increment list num_inc_list = range (#will) 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]]sa Mple_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], excluding 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) #删除第一次出现的该元素L from the list. COunt (Var) #该元素在列表中出现的个数L. 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]. with 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, the L1 operation is the operation of 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 C + + standard library) Dict = {' ob1′: ' 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 () D.items () d.update (DICT2) #增加合并字典D. Popitem () # Get a pair and remove it from the dictionary. Empty throws Exception D.clear () #清空字典, with Del dictd.copy () #拷贝字典D. CMP (DICT1,DICT2) #比较字典, (priority is the number of elements, key size, key value size) #第一个大返回1, small return-1, Returns the same as 0dictionary 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: WordString (that is, the character list cannot be modified) str = "Hello My friend" 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 in strstring module, also provides many 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]]) # Returns the number of substrings found S.lowercase () s.capitalize () #首字母大写S. Lower () #转小写S. Upper () #转大写S. Swapcase () #大小写互换S. Split (str, ') # A string to the list, S.join (list, ') #将list转string with a space to concatenate the built-in function of the string processing len (str) #串长度cmp ("My Friend", str) #字符串比较. First large, return 1max (' abcxyz ') #寻找字符串中最大的字符min (' abcxyz ') #寻找字符串中最小的字符string的转换oat (str) #变成浮点数, float ("1e-1″) result is 0.1int (str) # To Integer, the int ("12″) result is 12int (str,base) #变成base进制整型数, the Int (" 11″,2) result is 2long (str) #变成长整型, Long (str,base) #变成base进制长整型, Formatting of strings (note its escaped characters, mostly as in C, slightly) Str_format% (parameter list)

Python list array common operations collection "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.