[4.3] to achieve the objects can be sliced

. 1  # ! / User / bin / Python the env 
2  # - * - Coding: UTF-. 8 - * - 
. 3  
. 4  # Mode [start: End: STEP] 
. 5  "" " 
. 6      wherein the first number represents the start position of the slice start the default is 0;
 7      second number denotes end sections turned off (but not including) position (default list length);
 8      . the third number represents the slice step step (default 1)
 9      when the start time is 0 It may be omitted, when the end to be omitted when the length of the list
 10      when step 1 is omitted, and steps may be omitted when the last while omitting a colon.
 11      Further, when the step is negative integer, indicates a reverse slices, then should be larger than the start value of the end of the job.
 12 is  "" " 
13 is of aList = [. 3,. 4,. 5,. 6,. 7,. 9,. 11, 13 is, 15,. 17 ]
 14  # returns a list of all elements of the original new list 
15  Print (aList [::])
16  # returns a list of the original list of all of the elements in reverse order 
. 17  Print (of aList [:: -. 1 ])
 18 is  # compartments take a one acquired even positions of the elements 
. 19  Print (of aList [:: 2 ])
 20 is  # compartments take a an acquiring element odd position 
21 is  Print (of aList [. 1 :: 2 ])
 22 is  # specify start and end position of the slice 
23 is  Print (of aList [. 3:. 6 ])
 24  # at the end position of the slice is greater than the length of the list, from the end of the list cut 
25 of aList [0: 100 ]
 26 is  # when the slice start position is greater than the length of the list, an empty list 
27 of aList [100 :]
 28  
29  # adding elements to the tail of the list 
30of aList [len (of aList):] = [. 9 ]
 31 is  # is inserted in the list of the header element 
32 of aList [: 0] = [. 1, 2 ]
 33 is  # inserted intermediate position in the list element 
34 is of aList [. 3:. 3] = [. 4 ]
 35  # replacement list of elements, equal on both sides of equal length list 
36 of aList [:. 3] = [. 1, 2 ]
 37 [  # both sides of the equal sign may not be equal to the length of the list 
38 is of aList [. 3:] = [. 4,. 5, 6 ]
 39  # compartment a a modification 
40 of aList [:: 2] = [0]. 3 *
 41 is  Print (of aList)
 42 is  # compartment a a modification 
43 is of aList [:: 2] = [ ' a ' , ' B' , ' C ' ]
 44 is  # left slices are not continuous, equal on both sides must be equal to the length of the list 
45 of aList [:: 2] = [1,2 ]
 46 is  # delete the first three elements of the list 
47 of aList [:. 3] = []
 48  
49  # slicing successive elements 
50  del of aList [:. 3 ]
 51 is  # slice discontinuous elements, a puncturing a septum 
52 is  del of aList [:: 2]

The above is conventional slicing operation, an object that implements the following code may be a slice

 1 #!/user/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import numbers
 4 
 5 
 6 class Group:
 7     """
 8     支持切片操作
 9     """
10     def __init__(self, group_name, company_name, staffs):
11         self.group_name = group_name
12         self.company_name = company_name
13         self.staffs = staffs
14 
15     def __reversed__(self):
16         """
17         Reverse
 18 is          "" " 
. 19          self.staffs.reverse ()
 20 is  
21 is      DEF  the __getitem__ (Self, Item):
 22 is          " "" 
23 is          then implement this function, can slice a
 24          "" " 
25          CLS = type (Self)
 26 is          IF the isinstance (Item, Slice):
 27              return CLS (GROUP_NAME = self.group_name, COMPANY_NAME = self.company_name, Staffs = self.staffs [Item])
 28          elif the isinstance (Item, numbers.Integral):
 29              return CLS (= GROUP_NAME self.group_name, COMPANY_NAME = self.company_name, Staffs = [self.staffs [Item]])
30 
31     def __len__(self):
32         return len(self.staffs)
33 
34     def __iter__(self):
35         """
36         实现可迭代
37         """
38         return iter(self.staffs)
39 
40     def __contains__(self, item):
41         """
42         if 'zy' in group:
43             print('yes')
44         """
45         if item in self.staffs:
46             return True
47         else:
48             return False
49 
50 
51 staffs = ['zy1', 'zy2', 'zy3']
52 group = Group(group_name='user', company_name='imooc', staffs=staffs)
53 print(len(group))

 

Guess you like

Origin www.cnblogs.com/zydeboke/p/11247816.html