• # Intetools class

I watched your video on Python itertools. I found all of them and started writing a class to call them. Eventually, I will create a separate py for the itertools_class and put them in it. I also added in the doc entries.

``````#!/usr/bin/env/python3

from itertools import *

# Count() starts counting numbers
# Count only resets when you reset count()
def COUNT_FIRST5():
'''DOC: Counts from the starting number for a 5 count'''
iterator = count()
print("list first 5 numbers counted from  0:",
list(next(iterator) for _ in range(5)))
print("list next 5 numbers counted from  5:",
list(next(iterator) for _ in range(5)))
print("as you see I didn't reset count so it continues")
print("")

def COUNT_EVEN():
'''DOC: Counts from 0 first 5 even numbers'''
iterator = count(start=0, step=2)
print("list first 5 even numbers counted from 0:",
list(next(iterator) for _ in range(5)))
print("")

def COUNT_ODD():
'''DOC: Counts from 1 first 5 odd numbers'''
iterator = count(start=1, step=2)
print("list first 5 odd numbers counted from 1:",
list(next(iterator) for _ in range(5)))
print("")

def COUNT_FROMXBYYRANGEZ(x, y, z):
'''DOC: Count from x by y in a range of z'''
print("Count from {} by {} with RANGE {}".format(x, y, z))
iterator = count(start=x, step=y)
print("count by 5s:",
list(next(iterator) for _ in range(z)))
print("")

def COUNT_FROMXBYY(x, y):
'''DOC: Count up from x by y'''
iterator = count(start=x, step=y)
print("start at", x, "count by", y ,":",
list(next(iterator) for _ in range(y)))
print("")

def COUNT_FROMXBYMINUSY(x, y):
'''DOC: Count down from x by y'''
iterator = count(start=x, step=-y)
print("start at", x, "count by", -y ,":",
list(next(iterator) for _ in range(y)))
print("")

def COUNT_XSTARTSTEPYSTARTSTEPHOWMANY(xstart, xstep, ystart, ystep, pairs):
'''DOC: How do generate random points'''
counter = 0
print(
"start x: {} step x: {} start y: {} step y: {} how many pairs: {}".format(xstart, xstep, ystart, ystep, pairs))
x = count(start=xstart, step=xstep)
y = count(start=ystart, step=ystep)
points = zip(x, y)
for i in points:
if counter <= pairs:
print(list(i))
counter += 1
elif counter > pairs:
print("")
break

def REPEATXbyYTimes(x, y):
'''DOC: repeat x y times'''
print("repeat {} {} times".format(x, y))
repeat_num = repeat(x, y)
print(list(repeat_num))
print("")

def ACCUMULATE(iterable):
'''DOC: Accumulate a list of numbers or characters'''
x = iterable
accum_num = accumulate(x)
print("The list of {} accumulates to {}".format(x, list(accum_num)))
print("")

def CYCLEPOINTSXYZTIMES(x, y, pairs):
'''DOC: Cycle through two sets of points x times '''
print("Cycle throught", list(x), "and", list(y), "for", pairs, "interations")
cycle_pairs_x = cycle(x)
cycle_pairs_y = cycle(y)
points = zip(cycle_pairs_x, cycle_pairs_y)
counter = 0
for i in points:
if counter <= pairs:
print(list(i))
counter += 1
elif counter > pairs:
print("")
break

def main():
# chain is to combine two strings
chain_let = chain('ABCD', 'WXYZ')
print("chain('ABCD', 'WXYZ') combines both strings")
print(list(chain_let))

# chain.from_iterable takes a tuple and combines them
chain_iter_let = chain.from_iterable(['ABC', 'DEF'])
print("chain.from_iterable(['ABC', 'DEF'])) combines tuples")
print(list(chain_iter_let))
print("")
print("compress('TARBUCMDP', [1, 0, 1, 0, 1, 0, 1, 0, 1] will only print letters with 1")
compress_let = compress('TARBUCMDP', [1, 0, 1, 0, 1, 0, 1, 0, 1])
print(list(compress_let))

if __name__ == '__main__':
print('{}'.format(COUNT_FIRST5.__doc__))
COUNT_FIRST5()
print('{}'.format(COUNT_EVEN.__doc__))
COUNT_EVEN()
print('{}'.format(COUNT_ODD.__doc__))
COUNT_ODD()
print('{}'.format(COUNT_FROMXBYYRANGEZ.__doc__))
COUNT_FROMXBYYRANGEZ(7, 12, 10)
print('{}'.format(COUNT_FROMXBYY.__doc__))
COUNT_FROMXBYY(0, 5)
print('{}'.format( COUNT_FROMXBYMINUSY.__doc__))
COUNT_FROMXBYMINUSY(20, 5)
print('{}'.format(COUNT_XSTARTSTEPYSTARTSTEPHOWMANY.__doc__))
COUNT_XSTARTSTEPYSTARTSTEPHOWMANY(0.25, 0.25, 0.5, 0.25, 10)
print('{}'.format(REPEATXbyYTimes.__doc__))
REPEATXbyYTimes(20, 5)
REPEATXbyYTimes('ABC', 10)
print('{}'.format(ACCUMULATE.__doc__))
ACCUMULATE([1, 5, 9, 11, 13])
ACCUMULATE(['A', 'B', 'C', 'D'])
print('{}'.format(CYCLEPOINTSXYZTIMES.__doc__))
CYCLEPOINTSXYZTIMES([1, 2, 3, 4], [1, 2, 3], 20)

``````

Output.

``````/home/michael/PycharmProjects/itertools/venv/bin/python /home/michael/PycharmProjects/itertools/main.py
DOC: Counts from the starting number for a 5 count
list first 5 numbers counted from  0: [0, 1, 2, 3, 4]
list next 5 numbers counted from  5: [5, 6, 7, 8, 9]
as you see I didn't reset count so it continues

DOC: Counts from 0 first 5 even numbers
list first 5 even numbers counted from 0: [0, 2, 4, 6, 8]

DOC: Counts from 1 first 5 odd numbers
list first 5 odd numbers counted from 1: [1, 3, 5, 7, 9]

DOC: Count from x by y in a range of z
Count from 7 by 12 with RANGE 10
count by 5s: [7, 19, 31, 43, 55, 67, 79, 91, 103, 115]

DOC: Count up from x by y
start at 0 count by 5 : [0, 5, 10, 15, 20]

DOC: Count down from x by y
start at 20 count by -5 : [20, 15, 10, 5, 0]

DOC: How do generate random points
start x: 0.25 step x: 0.25 start y: 0.5 step y: 0.25 how many pairs: 10
[0.25, 0.5]
[0.5, 0.75]
[0.75, 1.0]
[1.0, 1.25]
[1.25, 1.5]
[1.5, 1.75]
[1.75, 2.0]
[2.0, 2.25]
[2.25, 2.5]
[2.5, 2.75]
[2.75, 3.0]

DOC: repeat x y times
repeat 20 5 times
[20, 20, 20, 20, 20]

repeat ABC 10 times
['ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC']

DOC: Accumulate a list of numbers or characters
The list of [1, 5, 9, 11, 13] accumulates to [1, 6, 15, 26, 39]

The list of ['A', 'B', 'C', 'D'] accumulates to ['A', 'AB', 'ABC', 'ABCD']

DOC: Cycle through two sets of points x times
Cycle throught [1, 2, 3, 4] and [1, 2, 3] for 20 interations
[1, 1]
[2, 2]
[3, 3]
[4, 1]
[1, 2]
[2, 3]
[3, 1]
[4, 2]
[1, 3]
[2, 1]
[3, 2]
[4, 3]
[1, 1]
[2, 2]
[3, 3]
[4, 1]
[1, 2]
[2, 3]
[3, 1]
[4, 2]
[1, 3]

Process finished with exit code 0

``````
• This is so awesome! Thank you so much for sharing this!

Anthony Sequeira
ITProTV Edutainer!

Posts 2Views 23