Analysis of list Operation instances in python Development

Source: Internet
Author: User
Tags python list
This article mainly introduces the list Operation Method for python development, and analyzes the specific usage and precautions of the list operation in the form of instances, if you need it, you can refer to the examples in this article to analyze the list Operation of python development. We will share this with you for your reference. The details are as follows:

For list operations in python, you can refer to "Python list Operation usage summary".

My personal notes are as follows:

# Python list ''' there are many ways to create a list: 1. use square brackets to create an empty list: [] 2. use square brackets to separate the elements in ',': [a, B, c], [a] 3. using a list comprehension: [x for x in iterable] 4. using the type constructor: list () or list (iterable) ''' def create_empty_list (): ''' Using a pair of square brackets to denote the empty list: []. '''return [] def create_common_list (): ''' Using square brackets, separating items with commas: [a], [a, B, c]. ''' re Turn ['A', 'B', 'C', 1, 3, 5] def create_common_list2 (): ''' Using a list comprehension: [x for x in iterable]. '''return [x for x in range (1, 10)] def str_to_list (s): ''' Using a string to convert list ''' if s! = None: return list (s) else: return [] def main (): test_listA = create_empty_list () print (test_listA) print ('#' * 50) test_listB = create_common_list () print (test_listB) print ('#' * 50) test_listC = create_common_list2 () print (test_listC) print ('#' * 50) test_str = 'I want to talk about this problem! 'Test_listd = str_to_list (test_str) print (test_listD) if _ name _ = '_ main _': main ()

Running effect:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> ================================ RESTART ================================>>> []##################################################['a', 'b', 'c', 1, 3, 5]##################################################[1, 2, 3, 4, 5, 6, 7, 8, 9]##################################################['i', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 't', 'a', 'l', 'k', ' ', 'a', 'b', 'o', 'u', 't', ' ', 't', 'h', 'i', 's', ' ', 'p', 'r', 'o', 'b', 'l', 'e', 'm', '!']>>> 

Here are more demos:

Python 3.3.2 (v3.3.2: d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> counter = 100 >>>> miles = 1000.0 >>>> name = "hongten" >>> numberA, numberB, nameC =, "Hongten" >>> list = [counter, miles, name, numberA, numberB, nameC]> print (list) [100,100 0.0, 'hongten ', 1, 2, 'hongten '] >>># this is the comments part. Use "#" to start with >>> for element in list: print (element) 1001000.0hongten12Hongten >>># list traversal list >>> print (list [0]) # obtain the first element value in the list: 100 >>> print (list [-1]) # obtain the last element value in the list: Hongten> print (len (list) # Use len (list) obtain the length of the list 6> num_inc_list = range (10) # generate a list with increasing values> print (num_inc_list) range (0, 10)> for inc_list in num_inc_list: print (inc_list) 0123456789 >>> # Here we can see range (10) is a list of incremental values from 0 to 9 >>> initial_value = 10 >>> list_length = 5 >>> myList = [initial_value for I in range (10)] >>> print (myList) [10, 10, 10, 10, 10, 10, 10, 10, 10] >>> list_length = 2 >>> myList = myList * list_length >>> print (myList) [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10] >>> print (len (myList )) 20 >>># use a fixed value initial_value to initialize a list myList >>># use myList = myList * list_length to copy myList >>## next let's take a look at the replication effect. >>> copyList = [1, 2, 3, "hongten"] >>> copyList = copyList * list_length >>> print (len (copyList) 8 >>> for cl in copyList: print (cl) 123hongten123hongten >>## next, let's take a closer look at the list in python >>># a list can contain different types of elements, which are similar to those in ActionScript 3.0 (AS3.0) the array in is similar to >>> test_list = ["hello", "world", "hongten"] >>> print (len (test_list )) 7 >>> print (test_list [0]) # print test_listhello >>># print the first element in test_list >>> print (test_list [-1]) # print the last element hongten> print (test_list [-len]) in test_list # print the first element Traceback (most recent call last): File"
 
  
", Line 1, in
  
   
Print (test_list [-len]) # print the first element TypeError: bad operand type for unary -: 'builtin _ function_or_method '> print (test_list [-len (test_list)]) # print the first element hello >>> print (test_list [len (test_list)-1]) # print the last element hongten> test_list.append (6) # append an element to the List> print (test_list [-1]) 6 >>> test_list.insert () >>> print (test_list) ['hello', 0, 1, 2, 'World', 4, 5, 'hongten ', 6] >>># the above operation is to insert element 0 to the location marked as 1 in the test_list. >>> test_list.insert () >>> print (test_list) ['hello', 0, 0, 1, 2, 'World', 4, 5, 'hongten ', 6] >>> test_list.insert () >>> print (test_list) ['hello', 0, 1, 0, 1, 2, 'World', 4, 5, 'hongten ', 6] >>> print (test_list.pop (0) # Return the last element and delete the hello >>> print (test_list) [0, 1, 0, 1, 2, 'World', 4, 5, 'hongten ', 6] >>> print (test_list.pop (2) # The comment above has an error, pop (index) returns the index element of the array and deletes it from the list.> print (test_list) [0, 1, 1, 2, 'World', 4, 5, 'hongten ', 6] >>> test_list.remove (1) >>> print (test_list) [0, 1, 2, 'World', 4, 5, 'hongten ', 6] >>## remove (1) indicates deleting the first element that appears. 1 >>> test_list.insert () >>> print (test_list) [1, 0, 1, 2, 'World', 4, 5, 'hongten ', 6] >>> test_list.remove (1) >>> print (test_list) [0, 1, 2, 'World', 4, 5, 'hongten ', 6] >>> test_list.insert (2, "hongten") >>> print (test_list) [0, 1, 'hongten ', 2, 'World', 4, 5, 'hongten', 6] >>> test_list.count ("hongten") 2 >>> # count (var) is the number of times the var element appears in the list >>> test_list.count ("foo") 0 >>> test_list_extend = ["a", "B ", "c"] >>> test_list.extend (test_list_extend) >>> print (test_list) [0, 1, 'hongten ', 2, 'World', 4, 5, 'hongten ', 6, 'A',' B ', 'C'] >>> # Use extend (list) append a list to the source list> print (test_list.sort () Traceback (most recent call last): File"
   
    
", Line 1, in
    
     
Print (test_list.sort () TypeError: unorderable types: str () <int () >>> test_list_extend.append ("h") >>> test_lsit_extend.append ("e ") traceback (most recent call last): File"
     
      
", Line 1, in
      
        Test_lsit_extend.append ("e") NameError: name 'test _ lsit_extend 'is not defined >>>> list_a = ["e", "z", "o ", "r"] >>> list_a.extend (test_list_extend) >>> print (list_a) ['E', 'z', 'O', 'R', 'A ', 'B', 'C', 'H']> print (list_a.sort ()) # sorting the list_a list. None >>## I don't know why the above sorting results contain errors... >>> list_ B = [,] >>>> print (list_ B .sort () None >>> print (sort (list_ B) Traceback (most recent call last): File"
       
         ", Line 1, in
        
          Print (sort (list_ B) NameError: name 'sort 'is not defined >>## skip sorting. Let's take a look at the delete operation. Print (list_ B) [1, 2, 3, 4, 5, 6] >>> print (del list_ B [1]) SyntaxError: invalid syntax >>> del list_ B [1] >>> print (list_ B) [1, 3, 4, 5, 6] >>> del list_ B [0, 2] Traceback (most recent call last): File"
         
           ", Line 1, in
          
            Del list_ B [] TypeError: list indices must be integers, not tuple >>> del list_ B [0: 2] >>> print (list_ B) [4, 5, 6] >>># del list [index] deletes the element whose subscript is index. del list [start: end] Delete the element from start subscript to end subscript >>> del list_ B [10] Traceback (most recent call last): File"
           
             ", Line 1, in
            
              Del list_ B [10] IndexError: list assignment index out of range >>> # If the subscript we delete exceeds the length range of the list, an error is reported. ######################################## ####################################>>> List_c = range (5); >>> for c in list_c: print (c) 01234 >>> list_d = list_c >>> for d in list_d: print (d) 01234 >>># copy the list >>> list_d [2] = 23 Traceback (most recent call last): File"
             
               ", Line 1, in
              
                List_d [2] = 23 TypeError: 'range' object does not support item assignment >>> list_e = [1, 2, 4, 4, 5] >>> list_f = list_e >>> list_f [2] = 234 >>> print (list_e) [1, 2,234, 4, 5] >>> # Here we can know that list_f copies list_e, and list_f is a reference to list_e. >>># they point to an object: [1, 2, 3, 4, 4, 5]. When we modify the value of list_f [2] In the view, >>> # the behavior of the object pointed to by list_f has changed, that is, the element value has changed, however, their references do not change. Therefore, list_e [2] = 234 is also reasonable. >>> ######################################## #################################>>> List_ I = list_e [:] >>> print (list_ I) [1, 2,234, 4, 5] >>> print (list_e) [1, 2,234, 4, 5] >>> list_ I [2] = 3 >>> print (list_e) [1, 2,234, 4, 5] >>> print (list_ I) [1, 2, 3, 4, 5] >>># clone the list, that is, copy another list, A new list object will be created >># so that list_ I and list_e point to different objects, there will be different references, so when list_ I [2] = 3, >>> # list_e [2] or equal to 234, that is, unchanged >>>
              
             
            
           
          
         
        
       
      
     
    
   
  
 

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.