python - Modify Itertools.cycle() -



python - Modify Itertools.cycle() -

i'm using itertools.cycle() object, , wondering if there anyway modify cycle after it's creation. following:

my_cycle = itertools.cycle([1,2,3]) print my_cycle.next() my_cycle.delete() #function doesn't exist print my_cycle.next()

would have output of:

1 3

is there way accomplish through itertools? or perhaps object? or need implement own object this.

itertools doesn't provide such option. can build deque:

from collections import deque class modifiablecycle(object): def __init__(self, items=()): self.deque = deque(items) def __iter__(self): homecoming self def __next__(self): if not self.deque: raise stopiteration item = self.deque.popleft() self.deque.append(item) homecoming item next = __next__ def delete_next(self): self.deque.popleft() def delete_prev(self): # deletes item returned. # suspect more useful other method. self.deque.pop()

python cycle itertools

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -