Linear table to delete the specified elements:
1 class deleelem_in_linelist(): 2 def __init__(self,list,maxsize): 3 self.data = list 4 self.length = len(list) 5 self.maxsize = maxsize 6 def dele_elem_in_list(self,e): 7 for i in self.data: 8 if i == e: 9 self.data.remove(i) 10 print("删除元素%s成功"%e) 11 # else: 12 is # Print (E% "% s does not exist a linear element table") 13 is return self.data 14 15 IF the __name__ == ' __main__ ' : 16 deleelem_in_linelist OB = ([. 1, 2,. 3,. 7,. 8,. 9, 10,. 11, 12 is, 13 is, 14], 20 is ) . 17 Print (ob.data) 18 is ob.dele_elem_in_list (15 ) . 19 Print (ob.data) 20 is ob.dele_elem_in_list (10 ) 21 is Print (ob.data)
Linear table specifies the location to remove elements:
1 "" " 2 linear delete elements in a table location . 3 " " ' . 4 class Solution (): . 5 DEF the __init__ (Self, Data, MAXSIZE): . 6 self.data = Data . 7 self.maxsize = MAXSIZE . 8 Self = .length len (Data) . 9 # delete a location element 10 DEF del_index_elem (Self, I): . 11 IF I> self.length or I <. 1 : 12 is Print ( " which can not delete element " ) 13 is the else : 14 E = self.data [-I. 1 ] 15 for J in Range (. 1-I,-self.length. 1 ): 16 self.data [J] = self.data [J +. 1 ] . 17 # the final position blank 18 is self.data [-self.length. 1] = None . 19 # linear length minus a table 20 is self.length -. 1 = 21 is return self.data 22 is 23 is IF the __name__ == ' __main__ ' : 24 OB = Solution ([ 1, 2, 3, 7, 8, 9, 10, 11, 12, 13, 14], 20 ) 25 # print(ob.data) 26 ob.del_index_elem(15) 27 print(ob.data) 28 ob.del_index_elem(6) 29 print(ob.data)