Python Forum
resetting an iterator to full
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
resetting an iterator to full
#1
if you iterate a list twice, the 2nd time has all the data from the beginning. but what if you have a case where the data can be figured out as an iterator and would be too large for memory, but you need to iterate it 2 or more times. an example test case would be range() or an equivalent that yields squares of sequence of ints (0,1,4,9,16,...)

for those who like to see code:
def range2(*a):
    for x in range(*a):
        yield x*x
            return None
yeah, that was absurdly simple.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Here is an example of an iterator but it doesn't sound like this is what your looking for. If it is not, can you be more specific about what your needs are.
class range2 :
	def __init__ (self, *numbers) :
		self.marker = -1 
		self.numbers = numbers

	def __iter__ (self) :
		return self

	def __next__ (self) :
		self.marker += 1
		if self.marker < len (self.numbers) :
			return self.numbers [self.marker] * self.numbers [self.marker]
		else :
			raise StopIteration

for test in range2 (1, 2, 3) :
	print (test, end = ' ')
print ()
for test in range2 (4, 5, 6) :
	print (test, end = ' ')
print ()
Reply
#3
Skaperen Wrote:but what if you have a case where the data can be figured out as an iterator and would be too large for memory, but you need to iterate it 2 or more times
Indeed, the answer is: write a generator. A generator is a compact representation of a potentially large or infinite sequence that can be iterated many times.
BashBedlam and Skaperen like this post
Reply
#4
yeah, it seems like a generator is the best way. but once it ends the iteration, how to start it, again, without creating a new one?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Feb-18-2022, 02:53 AM)Skaperen Wrote: yeah, it seems like a generator is the best way. but once it ends the iteration, how to start it, again, without creating a new one?
A generator is just a function. You restart it by simply calling it a second time. Here's an example:
def calculate_squares (seed) :
	while True :
		yield seed * seed
		seed += 1

square = calculate_squares (1)
for count in range (10) :
	print (next (square), end = ' ')
print ()

# Reset the generator
square = calculate_squares (3)
print (next (square))
Gribouillis likes this post
Reply
#6
in a case i was working with, i could only call it once. once called the returned generator was passed along and that code needed to iterate over it 3 times. the simple solution was to make a list and use that 3 times. that worked but there were a couple of huge cases that caused some big swapping.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
If you knew ahead of time at what point the generator should start over, I doesn't seem like it would be that difficult to write a generator function that you loop three times. Do you known what will trigger the generator to start over? A length, a flag, the StopIteration... ?
Reply
#8
it would be used in a for loop, so it would be StopIteration.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  prime numbers with iterator and generator cametan_001 8 1,869 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  popping an iterator Skaperen 11 3,701 Oct-03-2021, 05:08 PM
Last Post: Skaperen
  q re glob.iglob iterator and close jimr 2 2,235 Aug-23-2021, 10:14 PM
Last Post: perfringo
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 1,936 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Problem with an iterator grimm1111 9 4,318 Feb-06-2021, 09:22 PM
Last Post: grimm1111
  Multi-class iterator Pedroski55 2 2,387 Jan-02-2021, 12:29 AM
Last Post: Pedroski55
  is a str object a valid iterator? Skaperen 6 5,637 Jan-27-2020, 08:44 PM
Last Post: Skaperen
  discard one from an iterator Skaperen 1 1,991 Dec-29-2019, 11:02 PM
Last Post: ichabod801
  how do i pass duplicates in my range iterator? pseudo 3 2,356 Dec-18-2019, 03:01 PM
Last Post: ichabod801
  looking for a sprcil iterator Skaperen 7 3,358 Jun-13-2019, 01:40 AM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020