Python Iteration Through Lists with Stepped Progression -
given following lists:
letters = ('a', 'b', 'c', 'd', 'e', 'f', 'g') numbers = ('1', '2', '3', '4')
how can produce iterated list produces following:
output = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4'), ('e', '1'), ('f', '2'), ('g', '3'), ('a', '4'), ('b', '1'), ('c', '2'), ('d', '3'), ('e', '4'), ('f', '1'), ('g', '2')...]
i feel should able produce desired output using
output = (list(zip(letters, itertools.cycle(numbers))
but produces following:
output = [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4'), ('e', '1'), ('f', '2'), ('g', '3')]
any appreciated.
if looking infinite generator, can use cycle
zip
both lists, in form of zip(itertools.cycle(x), itertools.cycle(y))
. supply required generator:
>>> x in zip(itertools.cycle(letters), itertools.cycle(numbers)): ... print(x) ... ('a', '1') ('b', '2') ('c', '3') ('d', '4') ('e', '1') ('f', '2') ('g', '3') ('a', '4') ('b', '1') ('c', '2') ('d', '3') ...
Comments
Post a Comment