Python built-in function (-slice)

Source: Internet
Author: User

Official documents

class slice ( stop)
class slice ( start, stop[, step])
Return ASliceObject representing the set of indices specified by range(start, stop, step). The Startand StepArguments default to None. Slice objects has read-only data attributes start, stopand stepWhich merely return the argument values (or their default). They has no other explicit functionality; However they is used by numerical Python and all third party extensions. Slice objects was also generated when extended indexing syntax was used. For example: a[start:stop:step]Or a[start:stop, i]. See itertools.islice()For a alternate version that returns an iterator.
Description: 1. A function is actually a constructor for a slice class that returns a Slice object
2. The Slice object consists of 3 parameters, start, stop, step composition, start and step default is None. The slice object is primarily a slicing element on a Sequence object
>>>Help (Slice)|Return repr (self).| |Indices (...)| S.indices (len)(Start, stop, Stride)|      | Assuming a sequence of length Len, calculate the start andStop| Indices andThe stride length of the extended slice described by| S. Out of bounds indices is clippedincha manner consistent with the|handling of normal slices.|  | ---------------------------------------------------------------------- |Data descriptors defined here:| |Start| |Step| |Stop|  |  ---------------------------------------------------------------------- | Data andOther attributes defined here:| |__hash__= None

Li = [11,22,33,44,55,66,77]ret1= Li[none:5:none]#Start,stem to NoneRet2 = Li[:5:]#Ibid .Print(Ret1,ret2)#Output#[One, one, one, one, one, one, one, one, the other ]Ret3= Li[2:5:none]#step is NoneRet4 = Li[2:5:]Print(RET3,RET4)#Ibid .#Output#[From, to]#[From, to]Ret5= Li[1:6:3]Print(RET5)#Output#[A]

3.3 properties of the corresponding slice start, stop, and Step,slice functions also have 3 corresponding parameters start, stop, step, whose values are directly assigned to the slice object's start, stop, step

#Define C1C1 = Slice (5)Print(C1)#Output#Slice (None, 5, none)#Define C2C2 = Slice (2,5)Print(C2)#Output#Slice (2, 5, None)#define C3C3 = Slice (1,6,3)Print(C3)#Output#Slice (1, 6, 3)a= List (range (1,10)) Ret1= A[c1]#same as A[:5:]Print(RET1)#Output#[1, 2, 3, 4, 5]Ret2= A[c2]#same as A[2:5:]Print(Ret2)#Output#[3, 4, 5]Ret3= A[c3]#same as A[1:6:3]Print(RET3)#Output#[2, 5]

Python built-in function (-slice)

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.