popping an iterator - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: popping an iterator (/thread-35129.html) Pages:
1
2
|
popping an iterator - Skaperen - Oct-01-2021 i can easily extract the last item from a list with mylist.pop() . is there an easy way to do that with an iterator?
RE: popping an iterator - Yoriz - Oct-01-2021 import time.TimeMachine , use the future method to get the item at the end of the iterator.
RE: popping an iterator - bowlofred - Oct-01-2021 An iterator isn't a specific type of object. Lists are iterators. Some iterators have an accessible last item and some do not. You can iterate over an object like itertools.count which has no fixed length and no "end". So the most general answer is no. RE: popping an iterator - Skaperen - Oct-02-2021 (Oct-01-2021, 06:35 PM)Yoriz Wrote: import i guess this must run the iterator since it could be a generator or such. RE: popping an iterator - Skaperen - Oct-02-2021 (Oct-01-2021, 06:35 PM)Yoriz Wrote: import i don't seem to have time.TimeMachine. i'm still on Python 3.6. i hope to be on 3.9 soon. RE: popping an iterator - ndc85430 - Oct-02-2021 Somehow I don't think that was a serious suggestion.. RE: popping an iterator - ndc85430 - Oct-02-2021 Also, what would you think should happen if the iterator was infinitely long? It sounds like you've already come up with the solution to a problem, but it might help if you told us what the problem really is. Why do you want to do that? What are you trying to achieve, at a high level? RE: popping an iterator - DeaD_EyE - Oct-02-2021 Write your custom iterable/iterator from collections.abc import Sequence class SeekableIterator: def __init__(self, sequence): if not isinstance(sequence, Sequence): raise ValueError("No!") self.sequence = sequence self.index = 0 def seek(self, position): if 0 <= position <= len(self): self.index = position else: raise ValueError("Position out of range") def seek_relative(self, position): if 0 <= (new_index := self.index + position) <= len(self): self.index = new_index else: raise ValueError("Position out of range") @property def first(self): if self.sequence: return self.sequence[0] @property def last(self): if self.sequence: return self.sequence[-1] def __reversed__(self): """ Returns a new instance of SeekableIterator and the sequence is a reversed copy of the old """ return self.__class__(self.sequence[::-1]) def __len__(self): return len(self.sequence) def __iter__(self): return self.__class__(self.sequence) def __next__(self): if self.index <= len(self) - 1: value = self.sequence[self.index] self.index += 1 return value else: raise StopIteration iterator = SeekableIterator(["a", "b", "c"]) RE: popping an iterator - Skaperen - Oct-02-2021 i did find something called time_machine for CPython on Unix. it was a "PRE_LOAD" binary library that allowed setting fake clock values. it probably intercepted syscalls that get the time and added/subtracted a time offset. i think Linux can already do that in containers which would not let the same Python script control it (you'd have to create containers from Python and run something in them). RE: popping an iterator - Skaperen - Oct-02-2021 does reversed() work on iterators? ... nope! i think it's time for me to create a function named revit.
|