Guest User

Untitled

a guest
Dec 14th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. from itertools import cycle
  2. myIterator = cycle(range(2))
  3.  
  4. myIterator.next() # or next(myIterator) which works in Python 3.x. Yields 0
  5. myIterator.next() # or next(myIterator) which works in Python 3.x. Yields 1
  6. # etc.
  7.  
  8. from itertools import cycle
  9. mySmallSquareIterator = cycle(i*i for i in range(10))
  10. # Will yield 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 0, 1, 4, ...
  11.  
  12. >>> def alternate():
  13. ... while True:
  14. ... yield 0
  15. ... yield 1
  16. ...
  17. >>>
  18. >>> alternator = alternate()
  19. >>>
  20. >>> alternator.next()
  21. 0
  22. >>> alternator.next()
  23. 1
  24. >>> alternator.next()
  25. 0
  26.  
  27. import itertools
  28. myfunc = itertools.cycle([0,1]).next
  29.  
  30. myfunc() # -> returns 0
  31. myfunc() # -> returns 1
  32. myfunc() # -> returns 0
  33. myfunc() # -> returns 1
  34.  
  35. count = 0 # initialize count once
  36.  
  37. count = (count + 1) % 2
  38.  
  39. count = 0
  40.  
  41. for i in range(5):
  42. count = (count + 1) % 2
  43. print count
  44.  
  45. 1
  46. 0
  47. 1
  48. 0
  49. 1
  50.  
  51. var = not var
  52.  
  53. def alternate():
  54. alternate.x=not alternate.x
  55. return alternate.x
  56.  
  57. alternate.x=True #The first call to alternate will return False (0)
  58.  
  59. mylist=[5,3]
  60. print(mylist[alternate()]) #5
  61. print(mylist[alternate()]) #3
  62. print(mylist[alternate()]) #5
  63.  
  64. from itertools import cycle
  65.  
  66. alternator = cycle((0,1))
  67. next(alternator) # yields 0
  68. next(alternator) # yields 1
  69. next(alternator) # yields 0
  70. next(alternator) # yields 1
  71. #... forever
  72.  
  73. count = 1
  74. count = count ^ 1 # count is now 0
  75. count = count ^ 1 # count is now 1
  76.  
  77. var = 1
  78. var = 1 - var
  79.  
  80. value = (1, 0)[value]
  81.  
  82. toggle_val = 1
  83.  
  84. toggle_val = (1,0)[toggle_val]
  85.  
  86. a, b = map(int, raw_input("Enter both number: ").split())
  87. flag = input("Enter the first value: ")
  88. length = input("Enter Number of iterations: ")
  89. for i in range(length):
  90. print flag
  91. if flag == a:
  92. flag = b;
  93. else:
  94. flag = a
  95.  
  96. mode=["mouse",""].remove(mode)[0]
  97.  
  98. value=["option1","option2"].remove(value)[0]
Add Comment
Please, Sign In to add comment