所以不久前,我写了一个基本上可以满足您要求的课程。唯一的技巧是你必须向类传递每个数组的迭代器列表。
column_iter_traversal.py
"""
This code will take in a iterator of iterators. You can view the first
iterator as the rows of a graph (a matrix being a specific case of graphs)
and each iterable giving you the columns (or nodes) of that graph.
so if you have a graph
[[1, 2],
[3],
[4, 5]]
we'd expect the iterator to return [1, 3, 4, 2, 5]
"""
class ColumnTraversalIter():
"""
This is a class which is used to contain the currently travered state.
This is the class which defines the returned object for
column_traversal_iter. The iter that function returns is an instance of
this class.
"""
def __init__(self, iter_of_iters):
# Build a list of iterators
self.iter_list = []
for it in iter_of_iters:
self.iter_list.append(it)
self.current_iter_index = 0
def __iter__(self):
return self
def __next__(self):
# Get the next value from the current iterator
try:
return_val = next(self.iter_list[self.current_iter_index])
self.current_iter_index = self._increment_index(
self.current_iter_index,
len(self.iter_list))
return return_val
except StopIteration:
# When we run into a stop iteration we know that the current
# iterator is out of values. Remove the current iterator from
# the iterator list.
del self.iter_list[self.current_iter_index]
# If we are out of iterators it's time to raise StopIteration
if len(self.iter_list) == 0:
raise StopIteration
else:
# Otherwise, set the current_iter_index and recall next
self.current_iter_index = self._increment_index(
self.current_iter_index,
len(self.iter_list))
return self.__next__()
except IndexError:
# Someone called __next__ when there aren't any iterators left in
# the iter_list.
raise StopIteration
@staticmethod
def _increment_index(iter_index, wrap_length):
if iter_index + 1 > wrap_length:
print("returning 0")
return 0
else:
print("returning {}".format(iter_index + 1))
return iter_index + 1
def column_traversal_iter(iter_of_iters):
"""
args:
iterator: a iterator of iterators. If there aren't any iterators or
there are non iterator elements this will explode.
returns a COlumnTraversalIter
"""
return ColumnTraversalIter(iter_of_iters)
tests.py
import unittest
from column_traversal import column_traversal_iter
class TestBruteforceImplemetation(unittest.TestCase):
def test_no_iters(self):
test_iter = iter([])
column_iter = column_traversal_iter(test_iter)
with self.assertRaises(StopIteration):
next(column_iter)
def test_iter_of_one_empty_iter(self):
"""
One empty iter and many empty iters should hit one stop iteration.
"""
test_iter = iter([iter([])])
column_iter = column_traversal_iter(test_iter)
with self.assertRaises(StopIteration):
next(column_iter)
def test_iter_of_many_empty_iter(self):
"""
One empty iter and many empty iters should hit one stop iteration.
"""
test_iter = iter([iter([]), iter([]), iter([])])
column_iter = column_traversal_iter(test_iter)
with self.assertRaises(StopIteration):
next(column_iter)
def test_iter_simple_one_by_one_matrix(self):
"""
One empty iter and many empty iters should hit one stop iteration.
"""
test_iter = iter([iter([1]), iter([2]), iter([3])])
column_iter = column_traversal_iter(test_iter)
expected_traversal = [1, 2, 3]
for actual_value, expected_value in zip(column_iter, expected_traversal):
self.assertEqual(actual_value, expected_value)
# Check to make sure there's a stop iteration.
with self.assertRaises(StopIteration):
next(column_iter)
def test_iter_simple_jagged_graph(self):
"""
One empty iter and many empty iters should hit one stop iteration.
"""
test_iter = iter([iter([1]), iter([2, 4]), iter([3])])
column_iter = column_traversal_iter(test_iter)
expected_traversal = [1, 2, 3, 4]
for actual_value, expected_value in zip(column_iter, expected_traversal):
self.assertEqual(actual_value, expected_value)
# Check to make sure there's a stop iteration.
with self.assertRaises(StopIteration):
next(column_iter)
def test_iter_simple_two_by_two_matrix(self):
"""
One empty iter and many empty iters should hit one stop iteration.
"""
test_iter = iter([iter([1, 4]), iter([2, 5]), iter([3, 6])])
column_iter = column_traversal_iter(test_iter)
expected_traversal = [1, 2, 3, 4, 5, 6]
for actual_value, expected_value in zip(column_iter, expected_traversal):
self.assertEqual(actual_value, expected_value)
# Check to make sure there's a stop iteration.
with self.assertRaises(StopIteration):
next(column_iter)
def test_iter_one_iter_is_blank(self):
"""
One empty iter and many empty iters should hit one stop iteration.
"""
test_iter = iter([iter([1, 3]), iter([2, 4]), iter([])])
column_iter = column_traversal_iter(test_iter)
expected_traversal = [1, 2, 3, 4]
for actual_value, expected_value in zip(column_iter, expected_traversal):
self.assertEqual(actual_value, expected_value)
# Check to make sure there's a stop iteration.
with self.assertRaises(StopIteration):
next(column_iter)
你会使用这个代码
divisor_iter_list = []
for a_list in a_lists:
divisor_iter_list.append(iter(a_list))
dividend_iter_list = []
for b_list in b_lists:
divident_iter_list.append(iter(b_list))
divisor_iter = ColumnTraversalIter(divisor_iter_list)
dividend_iter = ColumnTraversalIter(divident_iter_list)
for divisor, dividend in zip(divisor_iter, dividend_iter):
# Do calculations.