#Slicing returns new elements without changing the original list
#Mode [start:end:step] ''' Where, the first number start indicates the start position of the slice, which is 0 by default The second number end indicates the cut-off (but not included) position of the slice (default is the list length) The third digit step represents the step size of the slice (default is 1) When start is 0, it can be omitted; when end is the list length, it can be omitted When the step is 1, the last colon can be omitted, and when the step is omitted, the last colon can be omitted at the same time In addition, when step is a negative integer, it means reverse slicing, which means that start should be larger than end. ''' aList = [3, 4, 5, 6, 7, 9, 11, 13, 15, 17] A list [::] ා returns a new list containing all elements in the original list A list [:: - 1] ා return the reverse list containing all elements in the original list Print (a list [:: 2]) takes one out of every interval to get even position elements Print (a list [1:: 2]) takes one interval and gets odd position elements Print (a list [3:6]) specifies the start and end positions of slices A list [0:100] ා when the end position of slice is greater than the length of the list, it is truncated from the end of the list A list [100:] ා when the start position of slice is greater than the list length, an empty list will be returned Value assignment A list [len (a list):] = [9] ා add elements at the end of the list A list [: 0] = [1,2] × add elements to the list header A list [3:3] = [4] × add elements in the middle of the list A list [: 3] = [1,2] replace the list element, and the length of the list on both sides of the equal sign is equal A list [3:] = [4,5,6] ා the length of the list on both sides of the equal sign can not be equal A list [:: 2] = ['a ','b','c '] (modify one element at an interval) #A list [:: 2] = [1,2] ා the left slice is not continuous, and the length of the list on both sides of the equal sign must be equal A list [: 3] = [] delete the first three elements in the list Delete Del a list [: 3] × slice elements continuously Del a list [:: 2] ා slice elements are not continuous, delete one every other
class Group: # Support slice operation def __init__(self,group_name,company_name,staffs): self.group_name = group_name self.company_name = company_name self.staffs = staffs def __reversed__(self): pass def __getitem__(self,item): # __getitem UUUUU magic function is the key to realize slicing. In fact, only realizing it can realize slicing return self.staffs[item] # Pass it to list and let list complete it def __len__(self): pass def __iter__(self): pass def __contains__(self,item): pass staffs = ["Tom1","Tom2","Tom3","Tom4"] group = Group(company_name = "MuKe", group_name="user",staffs=staffs) sub_group = group[:2] # At this point, after slicing, it is list, not group
How to implement, like an array using objects?
import numbers class Group: # Support slice operation def __init__(self,group_name,company_name,staffs): self.group_name = group_name self.company_name = company_name self.staffs = staffs def __reversed__(self): self.staffs.reverse def __getitem__(self,item): # __getitem UUUUU magic function is the key to realize slicing. In fact, only realizing it can realize slicing cls = type(self) if isinstance(item,slice): return cls(group_name=self.group_name,company_name=self.company_name,staffs=self.staffs[item]) elif isinstance(item,numbers.Integral): return cls(group_name=self.group_name, company_name=self.company_name, staffs=self.staffs[item]) def __len__(self): return len(self.staffs) def __iter__(self): return iter(self.staffs) def __contains__(self,item): if item in self.staffs: return True else: return False staffs = ["Tom1","Tom2","Tom3","Tom4"] group = Group(company_name = "MuKe", group_name="user",staffs=staffs) # sub_group = group[:2] # At this point, after slicing, it is list, not group # print(sub_group) # print(len(group)) # if "Tom" in group: # print("yes") # else: # print("No") reversed(group) for user in group: print(user)
254 original articles published, 112 praised, 100000 visitors+