Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. list = [0,0,0,1,2,3,4,0,0,0,5,6,7,8,9,0,0,0]
  2.  
  3.  
  4. class IteratorWithoutZeros:
  5. def __init__(self, my_list):
  6. self.list = my_list
  7. self.max = len(self.list)
  8. self.actual_position = 0
  9.  
  10. def __next__(self):
  11. self.actual_position = self.actual_position + 1
  12. while self.list[self.actual_position] == 0:
  13. self.actual_position = self.actual_position + 1
  14. if self.actual_position == self.max:
  15. raise StopIteration
  16. return self.list[self.actual_position]
  17.  
  18. def __iter__(self):
  19. return self
  20.  
  21.  
  22. class FullIterator:
  23. def __init__(self, my_list):
  24. self.list = my_list
  25. self.max = len(self.list)
  26. self.actual_position = 0
  27.  
  28. def __next__(self):
  29. self.actual_position = self.actual_position + 1
  30. if self.actual_position == self.max:
  31. raise StopIteration
  32. return self.list[self.actual_position]
  33.  
  34. def __iter__(self):
  35. return self
  36.  
  37.  
  38. iterator_without_zeros = IteratorWithoutZeros(list)
  39. full_iterator = FullIterator(list)
  40.  
  41. without_zeros_list = []
  42. full_list = []
  43.  
  44. for i in iterator_without_zeros:
  45. without_zeros_list.append(i)
  46.  
  47. for i in full_iterator:
  48. full_list.append(i)
  49.  
  50. print("Pełna lista: " + str(full_list))
  51. print("Lista bez zer: " + str(without_zeros_list))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement