KeyError when using Iterator class to Iterate through a DictSet - python

I've created a Set class that has the set types, ListSet and DictSet which are represented by a list and dictionary respectively. Both sets need to be iterable which I've created a SetIterator class to do so, however when I try testing DictSet, I get the following errors:
Traceback (most recent call last):
File "Set.py", line 118, in <module>
inter = e.intersection(t)
File "Set.py", line 22, in intersection
if element in other:
File "Set.py", line 8, in __next__
current_element = self.elements[self.index]
KeyError: 0
It seems as though there's an error within my SetIterator class, and I've tried rewriting the line I think is causing the error, but it hasn't fixed anything
def new(obj, *args, **kwargs):
return type(obj)(*args, **kwargs)
class SetIterator:
def __init__(self, s):
self.elements = s.members()
self.index = 0
def __next__(self):
try:
current_element = self.elements[self.index]
except IndexError:
raise StopIteration()
self.index += 1
return current_element
class Set:
def __iter__(self):
return SetIterator(self)
def intersection(self, other):
new_set = new(self) # to hold intersected set
for element in self:
if element in other:
new_set.add(element)
return new_set
def union(self, other):
new_set = new(self) # to hold unionized set
for element in self:
new_set.add(element)
for element in other:
if element not in new_set:
new_set.add(element)
return sorted(new_set)
def difference(self, other):
new_set = new(self) #to hold set difference
for element in self:
if element not in other:
new_set.add(element)
return new_set
def equals(self, other):
new_set = new(self)
other_set = new(self)
for element in self:
if element in other:
new_set.add(element)
for element in other:
if element in self:
other_set.add(element)
if new_set == other_set:
return True
else:
return False
def notEmpty(self):
empty_set = new(self)
placeholder = new(self)
for element in self:
placeholder.add(element)
if empty_set == placeholder: # If both are equal
return False # means self is empty
else:
return True # means self is not empty
class DictSet(Set):
def __init__(self, elements=[]):
rep = self.rep = {}
for element in elements:
rep[element] = element
def add(self, x):
self.rep[x] = x
# Testing code
if __name__ == '__main__':
e = DictSet([1, 2, 3, 4, 19, 31, 27, 0])
t = DictSet([1, 2, 3, 4])
print(t.rep)
# Testing DictSet
inter = e.intersection(t)
uni = e.union(t)
diff = e.difference(t)
eq = e.equals(t)
print('The intersection of the DictSets is:', inter)
print('The union of the Dictsets is:', uni)
print('The difference of the DictSets is:', diff)
print('Are the two DictSets equal?', eq)
I've tried rewriting SetIterator as:
class SetIterator:
def __init__(self, s):
self.elements = **s.rep**
self.index = 0
def __next__(self):
try:
current_element = self.elements[self.index]
except IndexError:
raise StopIteration()
self.index += 1
return current_element
However I still get the same error and my ListSet prints out the correct sets.

Related

class function to remove value from array list object

I am trying to write a class function that removes the first occurence of e (int number) from my array list and for it to return True but if no occurence then return false without adjustment to my array list.
def removeVal(self, e):
A = self.inArray
for x in A:
i+=1
if x == e:
A.remove(i)
self.inArray = A
return True
return False
list = [1,2,2,3,4,5]
list.removeVal(2)
print(list)
class ArrayList:
def __init__(self):
self.inArray = []
self.count = 0
def get(self, i):
return self.inArray[i]
def set(self, i, e):
self.inArray[i] = e
def length(self):
return self.count
def isIn(A, k): # similar to this
# for i in range(len(A)):
# if A[i] == k:
# return True
# return False
You can simply check if e is in the list. list.remove(x) removes the first occurence of x in the list.
You can switch out 'yourlist' with the list you are using.
def removeVal(self, e):
if e in yourlist:
yourlist.remove(e)
return True
return False

Add two list to one list

I'm trying to marge two lists that I created through a class to a new list. I use the __add__ function.
But it constantly adds the first two indexes and stops.
This is my code:
class Stack:
def __init__(self):
self.__items = []
self.__top = 0
def is_Empty(self):
return self.__top <= 0
try:
raise Exception('Stack empty.')
except Exception as error:
print(error)
def __str__(self):
"""Print current stack"""
if self.is_Empty() == True:
return "Stack empty"
else:
return "Stack is not empty"
def push(self, item):
"""Push item in stack."""
self.__items.append(item)
self.__top += 1
def pop(self):
"""Remove top of the stack."""
if self.__top <= 0:
return self.is_Empty()
self.__top -= 1
return self.__items.pop()
def top(self):
"""Return top of the stack."""
if self.__top <= 0:
return self.is_Empty()
else:
return self.__items[-1]
def my_stack(self):
"""Show the current stack"""
if self.__items == []:
return self.is_Empty()
else:
return f"The current stack is {self.__items}"
def __add__(self,other):
"""Add two lists together"""
newlst = []
for i, j in zip(self.__items, other.__items):
newlst.append(i+j)
return newlst
def __eq__(self, other):
"""Return True if two list is equal else Return False """
return (self is other) or (self.__items) == (other.__items)
for example:
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack2 = Stack()
stack2.push(4)
stack2.push(5)
stack2.push(6)
Now I'm trying to add the two stacks into stack3:
stack3 = stack + stack2
The result I get is this:
>>> print(stack3)
[5]
My goal is to get stack3 like this:
>>> print(stack3)
[1, 2, 3, 4, 5, 6]
Your current code
def __add__(self,other):
"""Add two lists together"""
newlst = []
for i, j in zip(self.__items, other.__items):
newlst.append(i+j)
return newlst
adds the first elements of each .__items list and returns the result after that.
To add the inner lists one after each other and return as list, this would work:
def __add__(self, other):
"""Add two lists together"""
if isinstance(other,Stack):
return self.__items + other.__items
return [] # or self.__items or raise Exception
To return a new Stack-instance you could do:
def __add__(self, other):
"""Add two lists together"""
if isinstance(other, Stack):
s = Stack()
for item in self.__items:
s.push(item)
for item in other.__items:
s.push(item)
return s
return [] # or self.__items or raise Exception

Next function on iterator stalls program

The program below stalls at line:
m = MapH()
This is related with the function:
def next(self):
If redefined as (should be only two underscores):
def __next__(self):
Then, got error message:
instance has no next() method
When hitting the line:
for e in m:
The full code is below:
class MapEntryH:
def __init__(self, key, value):
self.key = key
self.value = value
class MapIteratorH:
def __init__(self,entryList):
self._myEntryList = entryList
self._currItem = 0
def __iter__(self):
return self
def __next__(self):
if self._currItem < len(self._myEntryList):
item = self._myEntryList[self._currItem]
self._currItem += 1
return item
else:
StopIteration
class MapH:
def __init__(self):
self._entryList = list()
def __len__(self):
return len(self._entryList)
def _findPosition(self,key):
for i in range(len(self)):
if self._entryList[i].key == key:
return i
return None
def __contains__(self,key):
ndx = self._findPosition(key)
return ndx is not None
def add(self,key,value):
ndx = self._findPosition(key)
if ndx is not None:
self._entryList[ndx].value = value
return False
else:
entry = MapEntryH(key,value)
self._entryList.append(entry)
return True
def valueOf(self, key):
ndx = self._findPosition(key)
assert ndx is not None, "Invalid map key"
return self._entryList[ndx].value
def remove(self,key):
ndx =self._findPosition(key)
assert ndx is not None,"Invalid map key"
self._entryList.pop(ndx)
def __iter__(self):
return MapIteratorH(self._entryList)
def test_Map():
m = MapH()
m.add(1,"arg")
m.add(2,"eeu")
m.add(3,"ale")
m.add(4,"sue")
m.add(5,"bra")
temp = m.remove(5)
m.add(5,"chl")
m.add(5,"bol")
temp = m.valueOf(5)
temp = m._findPosition(4)
for e in m:
print(e)
me = MapEntryH(1,"arg")
test_Map()
How do I support iteration like:
for e in m:
print(e)
Or containment like:
me = MapEntryH(1,"arg")
if me in m:
print me.value + " is on the map"

Doubly-linked list in Python

It turned out to be an error as a time limit was exceeded,
but I've already raised the StopIteration...
I think I did something wrong for my iteration part, but it's really hard to find the error. The test output keeps running and even printed out the None value. How does it happen?
class LinkedListIterator:
def __init__(self, head):
self.__current = head.get_next()
def __iter__(self):
return self
def __next__(self):
if self.__current == None:
raise StopIteration
else:
item = self.__current.get_data()
self.__current = self.__current.get_next()
return item
These were the inputs I used to run the program:
my_list = LinkedListDLL()
my_list.add_to_head(1)
print("Contents:", end=" ")
for node in my_list:
print(node, end=" ")
print()
This code is meant to stop iteration when it reaches the head of the list.
if self.__current == None:
raise StopIteration
However, you represent the head with a NodeDLL object which is different from None.
You could keep a reference to the head and check against that instead:
class LinkedListIterator:
def __init__(self, head):
self._head = head
self._current = head.get_next()
def __iter__(self):
return self
def __next__(self):
if self._current is self._head:
raise StopIteration
else:
item = self._current.get_data()
self._current = self._current.get_next()
return item
What you want to implement is the API of a MutableSequence with the implementation of a doubly-linked-list.
To do that in Python, you should rely on collections.abc which can guide you through the process of implementing all required methods.
By example, a linked-list is actually a class inheriting from MutableSequence.
from collections.abc import MutableSequence
class LinkedList(MutableSequence):
pass
ll = LinkedList()
On instantiation of a class which has some abstract methods not yet written, you will get a TypeError which will guide you through which methods need to be implemented.
TypeError: Can't instantiate abstract class LinkedList with abstract methods __delitem__, __getitem__, __len__, __setitem__, insert
In particular, note that a list or a linked-list is not an iterator, it is an iterable. What this means is the __iter__ method should not return self and rely on __next__, it should instead return a brand new iterator on the content of the linked-list.
In other words, you can iterate only once through an iterator and multiple times through and iterable.
Full implementation
It turns out I have a full implementation of a doubly-linked-list implemented that way. You can have a look.
from collections.abc import MutableSequence
class LinkedList(MutableSequence):
class _Node:
def __init__(self, value, _next=None, _last=None):
self.value, self._next, self._last = value, _next, _last
def __str__(self):
return f'Node({self.value})'
def __init__(self, iterable=()):
self.start = None
self.last = None
empty = object()
iterable = iter(iterable)
first = next(iterable, empty)
if first is empty:
return
current = self._Node(first)
self.start, self.last = current, current
for value in iterable:
new_node = self._Node(value, _last=self.last)
self.last._next = new_node
self.last = new_node
def __len__(self):
if self.start is None:
return 0
else:
return sum(1 for _ in self)
def __iter_nodes(self):
current = self.start
while current is not None:
yield current
current = current._next
def __reversed_iter_nodes(self):
current = self.last
while current is not None:
yield current
current = current._last
def __iter__(self):
for node in self.__iter_nodes():
yield node.value
def __reversed__(self):
for node in self.__reversed_iter_nodes():
yield node.value
def __get_node(self, index):
if index >= 0:
for item in self.__iter_nodes():
if index == 0:
return item
index -= 1
else:
for item in self.__reversed_iter_nodes():
if index == 0:
return item
index += 1
raise IndexError
def __getitem__(self, index):
if index >= 0:
for item in self:
if index == 0:
return item.value
index -= 1
else:
for item in reversed(self):
if index == 0:
return item.value
index += 1
raise IndexError
def __setitem__(self, key, value):
self[key].value = value
def __delitem__(self, key):
node = self[key]
if node._last:
node._last._next = node._next
if node._next:
node._next._last = node._last
def insert(self, index, value):
if index > len(self):
self.last = self._Node(value, _last=self.last)
else:
where = self.__get_node(index)
_last = where._last
new_node = self._Node(value, _next=where, _last=_last)
if _last:
_last._next = new_node
else:
self.start = new_node
where._last = new_node
Example
ll = LinkedList(range(1, 5))
print(*ll)
print(*reversed(ll))
ll.insert(2, 'foo')
print(*ll)
Output
1 2 3 4
4 3 2 1
1 2 foo 3 4

Making a python iterator go backwards?

Is there anyway to make a python list iterator to go backwards?
Basically i have this
class IterTest(object):
def __init__(self, data):
self.data = data
self.__iter = None
def all(self):
self.__iter = iter(self.data)
for each in self.__iter:
mtd = getattr(self, type(each).__name__)
mtd(each)
def str(self, item):
print item
next = self.__iter.next()
while isinstance(next, int):
print next
next = self.__iter.next()
def int(self, item):
print "Crap i skipped C"
if __name__ == '__main__':
test = IterTest(['a', 1, 2,3,'c', 17])
test.all()
Running this code results in the output:
a
1
2
3
Crap i skipped C
I know why it gives me the output, however is there a way i can step backwards in the str() method, by one step?
EDIT
Okay maybe to make this more clear. I don't want to do a full reverse, basically what i want to know if there is an easy way to do the equivalent of a bidirectional iterator in python?
No, in general you cannot make a Python iterator go backwards. However, if you only want to step back once, you can try something like this:
def str(self, item):
print item
prev, current = None, self.__iter.next()
while isinstance(current, int):
print current
prev, current = current, self.__iter.next()
You can then access the previous element any time in prev.
If you really need a bidirectional iterator, you can implement one yourself, but it's likely to introduce even more overhead than the solution above:
class bidirectional_iterator(object):
def __init__(self, collection):
self.collection = collection
self.index = 0
def next(self):
try:
result = self.collection[self.index]
self.index += 1
except IndexError:
raise StopIteration
return result
def prev(self):
self.index -= 1
if self.index < 0:
raise StopIteration
return self.collection[self.index]
def __iter__(self):
return self
Am I missing something or couldn't you use the technique described in the Iterator section in the Python tutorial?
>>> class reverse_iterator:
... def __init__(self, collection):
... self.data = collection
... self.index = len(self.data)
... def __iter__(self):
... return self
... def next(self):
... if self.index == 0:
... raise StopIteration
... self.index = self.index - 1
... return self.data[self.index]
...
>>> for each in reverse_iterator(['a', 1, 2, 3, 'c', 17]):
... print each
...
17
c
3
2
1
a
I know that this doesn't walk the iterator backwards, but I'm pretty sure that there is no way to do that in general. Instead, write an iterator that walks a discrete collection in reverse order.
Edit you can also use the reversed() function to get a reversed iterator for any collection so that you don't have to write your own:
>>> it = reversed(['a', 1, 2, 3, 'c', 17])
>>> type(it)
<type 'listreverseiterator'>
>>> for each in it:
... print each
...
17
c
3
2
1
a
An iterator is by definition an object with the next() method -- no mention of prev() whatsoever. Thus, you either have to cache your results so you can revisit them or reimplement your iterator so it returns results in the sequence you want them to be.
Based on your question, it sounds like you want something like this:
class buffered:
def __init__(self,it):
self.it = iter(it)
self.buf = []
def __iter__(self): return self
def __next__(self):
if self.buf:
return self.buf.pop()
return next(self.it)
def push(self,item): self.buf.append(item)
if __name__=="__main__":
b = buffered([0,1,2,3,4,5,6,7])
print(next(b)) # 0
print(next(b)) # 1
b.push(42)
print(next(b)) # 42
print(next(b)) # 2
You can enable an iterator to move backwards by following code.
class EnableBackwardIterator:
def __init__(self, iterator):
self.iterator = iterator
self.history = [None, ]
self.i = 0
def next(self):
self.i += 1
if self.i < len(self.history):
return self.history[self.i]
else:
elem = next(self.iterator)
self.history.append(elem)
return elem
def prev(self):
self.i -= 1
if self.i == 0:
raise StopIteration
else:
return self.history[self.i]
Usage:
>>> prev = lambda obj: obj.prev() # A syntactic sugar.
>>>
>>> a = EnableBackwardIterator(iter([1,2,3,4,5,6]))
>>>
>>> next(a)
1
>>> next(a)
2
>>> a.next() # The same as `next(a)`.
3
>>> prev(a)
2
>>> a.prev() # The same as `prev(a)`.
1
>>> next(a)
2
>>> next(a)
3
>>> next(a)
4
>>> next(a)
5
>>> next(a)
6
>>> prev(a)
5
>>> prev(a)
4
>>> next(a)
5
>>> next(a)
6
>>> next(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
You can wrap your iterator in an iterator helper to enable it to go backward. It will store the iterated values in a collection and reuse them when going backwards.
class MemoryIterator:
def __init__(self, iterator : Iterator):
self._iterator : Iterator = iterator
self._array = []
self._isComplete = False
self._pointer = 0
def __next__(self):
if self._isComplete or self._pointer < len(self._array):
if self._isComplete and self._pointer >= len(self._array):
raise StopIteration
value = self._array[self._pointer]
self._pointer = self._pointer + 1
return value
try:
value = next(self._iterator)
self._pointer = self._pointer + 1
self._array.append(value)
return value
except StopIteration:
self._isComplete = True
def prev(self):
if self._pointer - 2 < 0:
raise StopIteration
self._pointer = self._pointer - 1
return self._array[self._pointer - 1]
The usage can be similar to this one:
my_iter = iter(my_iterable_source)
memory_iterator = MemoryIterator(my_iter)
try:
if forward:
print(next(memory_iterator))
else:
print(memory_iterator.prev())
except StopIteration:
pass
I came here looking for a bi-directional iterator. Not sure if this is what the OP was looking for but it is one way to make a bi-directional iterator—by giving it an attribute to indicate which direction to go in next:
class BidirectionalCounter:
"""An iterator that can count in two directions (up
and down).
"""
def __init__(self, start):
self.forward = True
# Code to initialize the sequence
self.x = start
def __iter__(self):
return self
def __next__(self):
if self.forward:
return self.next()
else:
return self.prev()
def reverse(self):
self.forward = not self.forward
def next(self):
"""Compute and return next value in sequence.
"""
# Code to go forward
self.x += 1
return self.x
def prev(self):
"""Compute and return previous value in sequence.
"""
# Code to go backward
self.x -= 1
return self.x
Demo:
my_counter = BidirectionalCounter(10)
print(next(my_counter))
print(next(my_counter))
my_counter.reverse()
print(next(my_counter))
print(next(my_counter))
Output:
11
12
11
10
i think thi will help you to solve your problem
class TestIterator():
def __init__(self):`
self.data = ["MyData", "is", "here","done"]
self.index = -1
#self.index=len(self.data)-1
def __iter__(self):
return self
def next(self):
self.index += 1
if self.index >= len(self.data):
raise StopIteration
return self.data[self.index]
def __reversed__(self):
self.index = -1
if self.index >= len(self.data):
raise StopIteration
return self.data[self.index]
r = TestIterator()
itr=iter(r)
print (next(itr))
print (reversed(itr))
ls = [' a', 5, ' d', 7, 'bc',9, ' c', 17, '43', 55, 'ab',22, 'ac']
direct = -1
l = ls[::direct]
for el in l:
print el
Where direct is -1 for reverse or 1 for ordinary.
Python you can use a list and indexing to simulate an iterator:
a = [1,2,3]
current = 1
def get_next(a):
current = a[a.index(current)+1%len(a)]
return current
def get_last(a):
current = a[a.index(current)-1]
return current # a[-1] >>> 3 (negative safe)
if your list contains duplicates then you would have to track your index separately:
a =[1,2,3]
index = 0
def get_next(a):
index = index+1 % len(a)
current = a[index]
return current
def get_last(a):
index = index-1 % len(a)
current = a[index-1]
return current # a[-1] >>> 3 (negative safe)
An iterator that visits the elements of a list in reverse order:
class ReverseIterator:
def __init__(self,ls):
self.ls=ls
self.index=len(ls)-1
def __iter__(self):
return self
def __next__(self):
if self.index<0:
raise StopIteration
result = self.ls[self.index]
self.index -= 1
return result
I edited the python code from dilshad (thank you) and used the following Python 3 based code to step between list item's back and forth or let say bidirectional:
# bidirectional class
class bidirectional_iterator:
def __init__(self):
self.data = ["MyData", "is", "here", "done"]
self.index = -1
def __iter__(self):
return self
def __next__(self):
self.index += 1
if self.index >= len(self.data):
raise StopIteration
return self.data[self.index]
def __reversed__(self):
self.index -= 1
if self.index == -1:
raise StopIteration
return self.data[self.index]
Example:
>>> r = bidirectional_iterator()
>>> itr=iter(r)
>>> print (next(itr))
MyData
>>> print (next(itr))
is
>>> print (next(itr))
here
>>> print (reversed(itr))
is
>>> print (reversed(itr))
MyData
>>> print (next(itr))
is
This is a common situation when we need to make an iterator go back one step. Because we should get the item and then check if we should break the loop. When breaking the loop, the last item may be requied in later usage.
Except of implementing an iteration class, here is a handy way make use of builtin itertools.chain :
from itertools import chain
>>> iterator = iter(range(10))
>>> for i in iterator:
... if i <= 5:
... print(i)
... else:
... iterator = chain([i], iterator) # push last value back
... break
...
0
1
2
3
4
5
>>> for i in iterator:
... print(i)
...
6
7
8
9
please see this function made by Morten Piibeleht. It yields a (previous, current, next) tuple for every element of an iterable.
https://gist.github.com/mortenpi/9604377

Categories