Python list built-in function usage instance analysis [insert, remove, index, pop, etc.], python instance analysis

Source: Internet
Author: User

Python list built-in function usage instance analysis [insert, remove, index, pop, etc.], python instance analysis

This example describes the usage of the Python list built-in functions. We will share this with you for your reference. The details are as follows:

# Coding = utf8''' standard type function: cmp (): The algorithm rule for sequence comparison is as follows: ------------------------------------------------------------------------------- 1. compare the two list elements. 2. if the element to be compared is of the same type, compare the value and return result 3. if the two elements are not of the same type, check whether they are numbers. if it is a number, execute the necessary number forced type conversion, and then compare. B. if one element is a number, the other element is "Big" (the number is "smallest") c. otherwise, the type names are compared in alphabetical order. 4. if a list first reaches the end, the other long list is "large" 5. if the list has the same length and all elements are equal, the two sequences are equal and a 0 value is returned. ---------------------------------------------------------------------------- Len (): returns the length of the string, that is, the number of characters contained in the string. For a list or tuples, it returns the number of elements in the list or tuples. Max () and min (): For string operations, it is the largest and smallest character in obtaining strings. For lists and element groups, it is used to obtain the largest and smallest elements in lists and element groups. The more complex the structure of a hybrid object, the worse the accuracy of the returned structure. Enumerate () and zip (): the former is the index and element value of the output list; the latter is a combination of elements corresponding to two long lists into a single tuples to generate a list of tuples. Sum () and reduce (): sums the number list. List () and tuple () accept iteratable objects as parameters, and create a new list or tuples by copying data in a shortest. If the range () function is not taken into account, python does not have any built-in functions specifically used for the list. The range () function accepts a value as the input and outputs a list that complies with the standard. List type built-in function list: ---------------------------------------------------------------------------------- list. append (obj) ------------------- add an object objlist to the list. count (obj) --------------------- returns the list of times an object obj appears in the list. extend (seq) ------------------ Add the sequence seq content to the list. index (obj, I = 0, j = len (list) ------ return the k value of list [k] = obj, and the range of k is in I <= k <J; otherwise, a ValueError exception is thrown. List. insert (index, obj) --------------- insert object obj at the position where the index volume is index. List. pop (index =-1) ----------------- Delete and return the object at the specified position. By default, it is the last Object list. remove (obj) ------------------- Delete the object objlist from the list. reverse () ----------------------- list the list in reverse order. sort (func = None, key = None, reverse = False) -------- sort the members in the list in the specified way. If the func and key parameters are specified, the elements are compared according to the specified method, if the reverser flag is set to True, the list is sorted in reverse order. Comment '''print "-------------- call the cmp () function ---------------" # Compare from the first element # if the elements are not equal, who has a large list will be large list1 = ["abcdef ", & quot; sunny & quot;, & quot; windy & quot;] list2 = [& quot; baby & quot;, & quot; godness & quot;, 123456] if cmp (list1, list2) <0: print list1 # Compare from the first element # if the elements are not equal, the list will be larger than list1 = ["zippo", "sunny ", "windy"] list2 = ["baby", "godness", 123456] if cmp (list1, list2)> 0: print list1 # consistent order can be completely equal # element order impact comparison result list1 = [1, 2, 3, 4, 5, 6, 7] list2 = [1, 2, 3, 4, 5, 6, 7] if cmp (list1, list2) = 0: print list1 # inconsistent number of elements # equal existing elements who have more elements and large list1 = [1, 2, 4, 4, 5] list2 = [1, 2, 3, 4, 5, 6, 7] if cmp (list1, list2) <0: print list1print "-------------------------------------" printprint "------------ call len () function ----------------- "# obtain the length of the element list list1 = [" baby "," godness ", 123456] print len (list1) print" inline "printprint" -------------- call max () and min (), sum () functions ----------------- "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 "plaintext" printprint "------------ call the enumerate () and zip () functions ---------------" list1 = ["zippo", "sunny", "windy", "one ", "two", "god", "witch"] list2 = [1, 2, 3, 4, 5, 6, 7] # Use the enumerate function to output element indexes and element values for ind, var in enumerate (list1): print ind, "------->", var # combine the elements corresponding to two long lists into a single tuple to generate a list of tuples for l1, l2 in zip (list1, list2): print "(", l1, l2, ")" print "---------------------------------------------" printprint "-------------- call list () and tuple () function --------------- "list1 = [" zippo "," sunny "," windy "," one "," two "," god "," witch "] list2 = [1, 2, 3, 4, 5, 6, 7] # Call the print type (list (list2) function of list () # Call the print type (tuple (list1) function of tuple )) print "outputs" printprint "-------------- built-in function of the list type -----------------------" list1 = ["zippo", "sunny", "windy", "one", "two", "god ", "witch"] list2 = [1, 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 The count () function print "The 3 appear times of list3:", list3.count (3) print "The windy appear times of list1:", list1.count ("windy") # Call The extend () function list1.extend (list2) print "add list2 to list1:", list1list2. extend ([12, 1, 6, 45]) print "add [12, 1, 6, 45] to list2:", list2 # Call index () function # sets the search range from the first element to the last element print "the index of one element in list1:", list1.index ("one ") # Set the search scope from 3rd elements to the last element print "the index of god element in list1:", list1.index ("god", 3) # Set the search range from 3rd elements to the fifth element print "the index of two element in list1:", list1.index ("two) # The index to be searched is not in The required range # A ValueError is thrown. try: print list1.index ("two", 5) failed t ValueError, v: print "The index is not range :", v # Call the insert () function # insert an object at the specified position # The function after the specified position moves one list2.insert (1, [123,45]) print "insert [123,45] into list2 at index = 1:", list2list2. insert (0, "hello") print "insert hello into list2 at index = 0:", list2 # call pop () function # Delete and return print "before calling pop (), the list2:", list2 # the last object print "the last element of list2:" by default :", list2.pop () print "after calling pop (), the list2:", list2 # Delete and return the third position element print "the third element of list2:", list2.pop (2) print "after calling pop (), the list2:", list2 # Call remove () # Delete the specified object from the list print "before calling remove (), the list3 :", list3 # Delete the 7list3 that appears for the first time from the first to the last position of list3. remove (7) print "after calling remove (), the list3:", list3 # call reverse () to print the list in reverse order "before calling reverse ():", list2list2. reverse () print "after calling reverse ():", list2 # Call the sort () queue sorting # without the list2.sort () print "calling sort () without parameter :", list2 # list2.sort (reverse = True) print "calling sort () with parameter reverse = True:", list2print "--------------------------------------print -----"

Running result:

For more Python-related content, refer to this topic: Python list) operation Skills summary, Python coding skills summary, Python data structure and algorithm tutorial, Python function usage skills summary, Python string operation skills summary, Python basic and advanced tutorial and Python file and directory operation tips

I hope this article will help you with Python programming.

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.