A detailed description of the use of the list () in Python's built-in functions

Source: Internet
Author: User
This article mainly introduces the Python list built-in function usage, combined with specific examples to analyze the list of INSERT, remove, index, pop and other functions, the use of methods and related considerations, the need for friends can refer to the following

This example describes the Python list built-in function usage. Share to everyone for your reference, as follows:


#coding =utf8 "' Standard type function: CMP (): The algorithm rules for sequence comparisons are as follows:-----------------------------------------------------------------------------1. Compares two elements of a list by 2. If the element being compared is of the same type, the value is compared and the result 3 is returned.  If the two elements are not of the same type, check whether they are number a. If it is a number, perform the necessary number to force the type conversion, and then compare. B. If one of the elements is a number, the other party's element is "large" (the number is the "smallest") c. Otherwise, the alphabetical order of the type name is compared by 4. If one list first reaches the end, the other long list is "large" 5. If the list is the same length and all elements are equal, then two sequences are equal, returning a 0. ------------------------------------------------------------------------------len (): Returns the length of the string, which is the number of characters that the string contains. For a list or tuple, it returns the number of elements in the list or tuple. Max () and Min (): For string manipulation, gets the maximum and minimum characters in a string, and for lists and tuples, gets the largest and smallest elements in the list and tuple. The more complex the structure of a mixed object, the worse the structural accuracy of the returned structure. Enumerate () and zip (): The former is the index and element value of the output list, the latter equal to two lists of elements that correspond to a tuple, generating a list of tuples. SUM () and reduce (): Sums the list of numbers. List () and tuple () accept an iterative object as an argument and create a new list or tuple by shallow copying the data. If you do not consider the range () function, there are no built-in functions in Python that are specific to the list. The range () function takes a numeric value as input and outputs a list that conforms to the criteria. List type built-in function list:--------------------------------------------------------------------------------list.append (obj)--- ----------------Adding an Object Objlist.count (obj) to the list---------------------returns the number of occurrences of an object, obj, in the list list.extend (seq)----------- ---------Adds the contents of the sequence SEQ to the list list.index (Obj,i=0,j=len (list))------Returns the K value of List[k]==obj, and the range of K is i<=k<j; otherwise ValueError abnormal hair. List.insert (index,obj)---------------inserts the object obj at the index position. List.pop (index=-1)-----------------deletes and returns the object at the specified position, which is the last object List.remove (obj) by default------------------- Remove an object from the list objlist.reverse ()-----------------------Reverse the list list.sort (func=none,key=none,reverse=false)-------- Sorts the members in the list in the specified manner, if the Func and key parameters are specified, compares the individual elements in the specified manner, and if the REVERSER flag is set to TRUE, the column The tables are arranged in reverse order. --------------------------------------------------------------------------------' print '--------------call CMP () function-----------------"#从第一个元素开始比较 # If the elements are not equal, the big list is big list1=[" abcdef "," Sunny "," Windy "]list2=[" Baby "," godness ", 123456] If CMP (LIST1,LIST2) <0:print list1# starts from the first element # if the elements are not equal, the large list is large list1=["Zippo", "Sunny", "Windy"]list2=["Baby", " Godness ", 123456]if cmp (LIST1,LIST2) >0:print list1# Order exactly equal # Element order affects comparison results list1=[1,2,3,4,5,6,7]list2=[ 1,2,3,4,5,6,7]if CMP (LIST1,LIST2) ==0:printlist1# element number is inconsistent # There are elements equal to whose elements are larger list1=[1,2,3,4,5]list2=[1,2,3,4,5,6,7]if cmp (LIST1,LIST2) <0:print list1print "----- ------------------------------------------"Printprint"--------------call the Len () function-----------------"#获取元素列表长度list1 = ["Baby", "godness", 123456]print len (list1) print "-----------------------------------------------" printprint "----- ---------call Max () and Min (), sum () function-----------------"list1=[" Zippo "," Sunny "," Windy "]list2=[1,2,3,4,5,6,7]print" List2 Max: ", Max (list2)," \ T "," List2 min: ", min (list2) print" List1 max: ", Max (list1)," \ T "," List1 min: ", min (list1) print" The List2 sum: ", sum (list2) print"-----------------------------------------------"Printprint"-------------- Call enumerate () with the zip () function-----------------"list1=[" Zippo "," Sunny "," windy "," one "," one "," God "," witch "]list2=[ 1,2,3,4,5,6,7] #使用enumerate函数输出元素索引和元素值for Ind,var in Enumerate (LIST1): Print ind, "------->", var# The equal-length two lists correspond to elements grouped into a tuple, generating a tuple list for l1,l2 in Zip (list1,list2): Print "(", L1,l2, ")" Print "------------------------------ -----------------"Printprint"--------------call list () and the tuple () function-----------------"list1=[" Zippo "," Sunny "," windy "," one "," one " , "God", "witch"]list2=[1,2,3,4,5,6,7] #调用list () function print type (list (list2)) #调用tuple () function print type (tuple (list1)) print " -----------------------------------------------"Printprint"--------------List-type built-in function-------------------------" list1=["Zippo", "Sunny", "windy", "one", "one", "God", "witch"]list2=[1,2,3,4,5,6,7]list3=list (list2*3) print "copy List2 3 times to List3: ", list3# call Append () function list2.append (8) print" Add 8 to List2 with append (): ", list2# call count () function print" T He 3 appear times of List3: ", List3.count (3) print" The windy appear times of List1: ", List1.count (" Windy ") #调用extend () function list 1.extend (list2) print "Add List2 to List1:", List1list2.extend ([12,1,6,45]) print "Add [12,1,6,45] to List2:", list2# Call the index () function # to set the lookup range from the first element to the last element print "The index of one element in List1:", List1.index ("one") # Set the lookup range from 3rd element to last element print "The index of God element in List1:", List1.index ("God", 3) #设置查找范围是从第3个元素到第五个元素print "The index of both element in List1:", List1.index ("a", 3,5) #要查找的index不在所需范围内 # throws a ValueError exception Try:print List1.index (" ", 5) except Valueerror,v:print" The index is not range: ", v# calls the Insert () function # Inserts an object at the specified position # The function after the specified position is moved back one List2.insert (1,[ 123,45]) print "Insert [123,45] into list2 at index=1:", List2list2.insert (0, "hello") print "Insert Hello to List2 at index =0: ", list2# call the Pop () function # to delete and return the object at the specified position print" before calling pop (), the List2: ", list2# default is the last object print" The end element of List2: ", List2.pop () print" after calling pop (), the List2: ", list2# Delete and return elements of the third position print" The third element of List2: ", List2.pop (2) print "After calling pop (), the List2:", list2# call Remove () #从列表中删除指定对象print "before calling Remove (), the LIST3 : ", list3# delete list3 from first to last, first appeared 7list3.remove (7) print" After calling Remove (), the List3: ", list3# call reverse () Reverse list print "before calling reverse ():", List2list2.reverse () print "After calling reverse ():", list2# call sort () queue ordering # No parameter List2.sort () print "calling sort () without parameter:", list2# reverse list list2.sorT (reverse=true) print "calling sort () with parameter reverse=true:", List2print "------------------------------------- --------------------"Print

Operation Result:

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.