https://blog.csdn.net/heifan2014/article/details/78719369

https://www.cnblogs.com/lulipro/p/5052619.html

Use the index to obtain a list of elements (random read)

 

   List elements supported by index access, forward index starting from 0

            

   colors=["red","blue","green"]

      colors[0] =="red"

      colors[1]=="blue"

            

   Also, you can use negative index (python support in an ordered sequence of negative index)

            colors[-1]=="green"

          

       

 

Slicing list

  Slicing operation is not peculiar to the list, an ordered sequence Python support sections, such as a string, a tuple.

  Slices and slice type consistent with the results returned object type, the sequence returns slice objects, such as: a list of slice returns a list,

  Returns a string string sections.

  Slice sequence elements are generated version of the copy source. Thus the slice is a shallow copy.

  

  li=["A","B","C","D"]

  

  Format: li [start: end: step]    

 start is the starting point for the slice index, end index is sliced ​​end, but the end result value slice index is not included. step is the step size is 1 by default.

       

 

 

      t = li [0: 3] [ "A", "B", "C"] # starting index of 0 can be omitted, t = li [: 3]

       t = li [2:] [ "C", "D"] # end is omitted, then cut to the end

      t=li[1:3]        ["B","C"]

      

      t = li [0: 4: 2] [ "A", "C"] # from li [0] to li [3], the step size is set to 2.

                    

       

  

 How to determine the start and end, they are what is the relationship?

 

       Under certain circumstances the symbol step, start and end can be mixed using forward and reverse index, no matter what, you must ensure that

      Between start and end elements in the same direction and step interval, otherwise it will be cut out empty list

 

            t = li [0: 2]

            t = li [0: -2]

            t = li [-4: -2]

            t = li [-4: 2]

             

            The above results are the same; t is [ "A", "B"]

 

      

      

         t = li [-1: -3: -1]

         t = li [-1: 1: -1]

         t = li [3: 1: -1]

         t = li [3: -3: -1]

 

         The above results are the same; t is [ "D", "C"]

      

  

       

          

         t = li [-1: -3]

         t = li [-1: 1]

         t = li [3: 1]

         t = li [3: -3]

         They are cut out empty list

           

             

 

    

      At the same time, step determines the positive and negative results of elementary sections have collected

               

 

      Omit start and end expressed in the original list of all target

 

      t = li [:: - 1] t ---> [ "C", "B", "A"] # reverse cut, cut out all

                   

 

      t=li[:]        t--->["A","B","C","D"]   #正向切全部

Guess you like

Origin www.cnblogs.com/JavaJavajava/p/11078889.html