6/07/2020

Python - Itertools Module : Iterator Functions for Efficient Looping

** Itertools Module : Iterator Functions for Efficient Looping


==========================================

import itertools

counter = itertools.count()

for num in counter:
print(num)

---------------------------------

0
1
2
3
... 정지 명령을 내릴때 까지 계속 1씩 증가

==========================================
==========================================

import itertools

counter = itertools.count()

print(next(counter))

---------------------------------

0

---------------------------------

import itertools

counter = itertools.count()

print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))

---------------------------------

0
1
2
3

==========================================
==========================================

import itertools

counter = itertools.count()

data = [100, 200, 300, 400]

daily_data = list(zip(itertools.count(), data))

print(daily_data)

---------------------------------

[(0, 100), (1, 200), (2, 300), (3, 400)]

==========================================
==========================================

import itertools

counter = itertools.count(start=5)

print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))

---------------------------------

5
6
7
8

---------------------------------

import itertools

counter = itertools.count(start=5, step=5)

print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))

---------------------------------

5
10
15
20

---------------------------------

import itertools

counter = itertools.count(start=5, step=-2.5)

print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))

---------------------------------

5
2.5
0.0
-2.5

==========================================
==========================================







import itertools

data = [100, 200, 300, 400]

daily_data = list(zip(range(10), data))

print(daily_data)

---------------------------------

[(0, 100), (1, 200), (2, 300), (3, 400)]

---------------------------------

import itertools

data = [100, 200, 300, 400]

daily_data = list(itertools.zip_longest(range(10), data))

print(daily_data)

---------------------------------

[(0, 100), (1, 200), (2, 300), (3, 400), (4, None), (5, None), (6, None), (7, 

None), (8, None), (9, None)]

==========================================
==========================================

import itertools

counter = itertools.cycle([1, 2, 3])

print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))

---------------------------------

1
2
3
1
2
3

---------------------------------

import itertools

counter = itertools.cycle(('On', 'Off'))

print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))

---------------------------------

On
Off
On
Off
On
Off

---------------------------------

import itertools

counter = itertools.repeat(2, times=3)

print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))
print(next(counter))

---------------------------------

2Traceback (most recent call last):
  File "C:\Users\purunet\Documents\py9\ii.py", line 9, in <module>

2
2
    print(next(counter))
StopIteration

==========================================
==========================================

import itertools

counter = itertools.repeat(2, times=3)

squares = map(pow, range(10), itertools.repeat(2))

print(list(squares))

---------------------------------

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

---------------------------------

import itertools

counter = itertools.repeat(2, times=3)

squares = itertools.starmap(pow, [(0, 2), (1, 2), (2, 2)])

print(list(squares))

---------------------------------

[0, 1, 4]

==========================================
==========================================

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

result = itertools.combinations(letters, 2)

for item in result:
print(item)

---------------------------------

('a', 'b')
('a', 'c')
('a', 'd')
('b', 'c')
('b', 'd')
('c', 'd')

---------------------------------

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

result = itertools.permutations(letters, 2)

for item in result:
print(item)

---------------------------------

('a', 'b')
('a', 'c')
('a', 'd')
('b', 'a')
('b', 'c')
('b', 'd')
('c', 'a')
('c', 'b')
('c', 'd')
('d', 'a')
('d', 'b')
('d', 'c')

---------------------------------

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

result = itertools.product(numbers, repeat=4)

for item in result:
print(item)

---------------------------------

(0, 0, 0, 0)
(0, 0, 0, 1)
(0, 0, 0, 2)
(0, 0, 0, 3)
(0, 0, 1, 0)
(0, 0, 1, 1)
(0, 0, 1, 2)
(0, 0, 1, 3)
(0, 0, 2, 0)
(0, 0, 2, 1)
(0, 0, 2, 2)
(0, 0, 2, 3)
(0, 0, 3, 0)
(0, 0, 3, 1)
(0, 0, 3, 2)
(0, 0, 3, 3)
(0, 1, 0, 0)
(0, 1, 0, 1)
(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, 3, 2)
(3, 3, 3, 3)

---------------------------------

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

result = itertools.combinations_with_replacement(numbers, 4)

for item in result:
print(item)

---------------------------------

(0, 0, 0, 0)
(0, 0, 0, 1)
(0, 0, 0, 2)
(0, 0, 0, 3)
(0, 0, 1, 1)
(0, 0, 1, 2)
(0, 0, 1, 3)
(0, 0, 2, 2)
(0, 0, 2, 3)
(0, 0, 3, 3)
(0, 1, 1, 1)
(0, 1, 1, 2)
(0, 1, 1, 3)
(0, 1, 2, 2)
(0, 1, 2, 3)
(0, 1, 3, 3)
(0, 2, 2, 2)
(0, 2, 2, 3)
(0, 2, 3, 3)
(0, 3, 3, 3)
(1, 1, 1, 1)
(1, 1, 1, 2)
(1, 1, 1, 3)
(1, 1, 2, 2)
(1, 1, 2, 3)
(1, 1, 3, 3)
(1, 2, 2, 2)
(1, 2, 2, 3)
(1, 2, 3, 3)
(1, 3, 3, 3)
(2, 2, 2, 2)
(2, 2, 2, 3)
(2, 2, 3, 3)
(2, 3, 3, 3)
(3, 3, 3, 3)

==========================================
==========================================

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

combined = itertools.chain(letters, numbers, names)

for item in combined:
print(item

---------------------------------

a
b
c
d
0
1
2
3
LinuxerHAN
SeokDu

==========================================
==========================================

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

result = itertools.islice(range(10), 5)

for item in result:
print(item)

---------------------------------

0
1
2
3
4

---------------------------------

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

result = itertools.islice(range(10), 1, 5)

for item in result:
print(item)

---------------------------------

0
1
2
3
4

---------------------------------

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

result = itertools.islice(range(10), 1, 5, 2)

for item in result:
print(item)
---------------------------------

1
3

==========================================
==========================================

import itertools

with open('test.log', 'r') as f:
header = itertools.islice(f, 3)

for line in header:
print(line)

---------test.log-------------------

Date: 2020-06-07
Author: LinuxerHAN
Description: This is a sample log file

Okay, so this is a sample entry.
I'm going to write a few more lines here.
For the sake of this video, let's pretend this log file is thousands 

and thousands of lines... okay?

---------------------------------

Date: 2020-06-07

Author: LinuxerHAN

Description: This is a sample log file

---------------------------------

import itertools

with open('test.log', 'r') as f:
header = itertools.islice(f, 3)

for line in header:
print(line)

---------test.log-------------------

Date: 2020-06-07
Author: LinuxerHAN
Description: This is a sample log file

Okay, so this is a sample entry.
I'm going to write a few more lines here.
For the sake of this video, let's pretend this log file is thousands 

and thousands of lines... okay?

---------------------------------

Date: 2020-06-07

Author: LinuxerHAN

Description: This is a sample log file

---------------------------------

import itertools

with open('test.log', 'r') as f:
header = itertools.islice(f, 3)

for line in header:
print(line, end='')

---------test.log-------------------

Date: 2020-06-07
Author: LinuxerHAN
Description: This is a sample log file

Okay, so this is a sample entry.
I'm going to write a few more lines here.
For the sake of this video, let's pretend this log file is thousands 

and thousands of lines... okay?

---------------------------------

Date: 2020-06-07
Author: LinuxerHAN
Description: This is a sample log file

==========================================
==========================================

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

selectors = [True, True, False, True]

result = itertools.compress(letters, selectors)

for item in result:
print(item)

---------------------------------
a
b
d

==========================================
==========================================

import itertools

def lt_2(n):
if n < 2:
return True
return False

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

selectors = [True, True, False, True]

result = filter(lt_2, numbers)

for item in result:
print(item)

---------------------------------

0
1

---------------------------------

import itertools

def lt_2(n):
if n < 2:
return True
return False

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3]
names = ['LinuxerHAN', 'SeokDu']

selectors = [True, True, False, True]

result = itertools.filterfalse(lt_2, numbers)

for item in result:
print(item)

---------------------------------

2
3

---------------------------------

import itertools

def lt_2(n):
if n < 2:
return True
return False

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3, 2, 1, 0]
names = ['LinuxerHAN', 'SeokDu']

selectors = [True, True, False, True]

result = itertools.filterfalse(lt_2, numbers)

for item in result:
print(item)

---------------------------------

2
3
2

---------------------------------

import itertools

def lt_2(n):
if n < 2:
return True
return False

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3, 2, 1, 0]
names = ['LinuxerHAN', 'SeokDu']

selectors = [True, True, False, True]

result = itertools.dropwhile(lt_2, numbers)

for item in result:
print(item)

---------------------------------

2
3
2
1
0

---------------------------------

import itertools

def lt_2(n):
if n < 2:
return True
return False

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3, 2, 1, 0]
names = ['LinuxerHAN', 'SeokDu']

selectors = [True, True, False, True]

result = itertools.takewhile(lt_2, numbers)

for item in result:
print(item)

---------------------------------

0
1

==========================================
==========================================

import itertools

letters = ['a', 'b', 'c', 'd']
numbers = [0, 1, 2, 3, 2, 1, 0]
names = ['LinuxerHAN', 'SeokDu']

result = itertools.accumulate(numbers)

for item in result:
print(item)

---------------------------------

0
1
3
6
8
9
9

---------------------------------

import itertools
import operator


letters = ['a', 'b', 'c', 'd']
numbers = [1, 2, 3, 2, 1, 0]
names = ['LinuxerHAN', 'SeokDu']

result = itertools.accumulate(numbers, operator.mul)

for item in result:
print(item)

---------------------------------

1
2
6
12
12
0

==========================================
==========================================

import itertools


def get_state(person):
    return person['state']


people = [
    {
        'name': 'Linuxer HAN',
        'city': 'Gotham',
        'state': 'NY'
    },
    {
        'name': 'SeokDu HAN',
        'city': 'Kings Landing',
        'state': 'NY'
    },
    {
        'name': 'Corey Schafer',
        'city': 'Boulder',
        'state': 'CO'
    },
    {
        'name': 'Al Einstein',
        'city': 'Denver',
        'state': 'CO'
    },
    {
        'name': 'John Henry',
        'city': 'Hinton',
        'state': 'WV'
    },
    {
        'name': 'Randy Moss',
        'city': 'Rand',
        'state': 'WV'
    },
    {
        'name': 'Nicole K',
        'city': 'Asheville',
        'state': 'NC'
    },
    {
        'name': 'Jim Doe',
        'city': 'Charlotte',
        'state': 'NC'
    },
    {
        'name': 'Jane Taylor',
        'city': 'Faketown',
        'state': 'NC'
    }
]

person_group = itertools.groupby(people, get_state)

for key, group in person_group:
    print(key)
    for person in group:
        print(person)
    print()

---------------------------------

NY
{'name': 'Linuxer HAN', 'city': 'Gotham', 'state': 'NY'}
{'name': 'SeokDu HAN', 'city': 'Kings Landing', 'state': 'NY'}

CO
{'name': 'Corey Schafer', 'city': 'Boulder', 'state': 'CO'}
{'name': 'Al Einstein', 'city': 'Denver', 'state': 'CO'}

WV
{'name': 'John Henry', 'city': 'Hinton', 'state': 'WV'}
{'name': 'Randy Moss', 'city': 'Rand', 'state': 'WV'}

NC
{'name': 'Nicole K', 'city': 'Asheville', 'state': 'NC'}
{'name': 'Jim Doe', 'city': 'Charlotte', 'state': 'NC'}
{'name': 'Jane Taylor', 'city': 'Faketown', 'state': 'NC'}

---------------------------------

import itertools


def get_state(person):
    return person['state']


people = [
    {
        'name': 'Linuxer HAN',
        'city': 'Gotham',
        'state': 'NY'
    },
    {
        'name': 'SeokDu HAN',
        'city': 'Kings Landing',
        'state': 'NY'
    },
    {
        'name': 'Corey Schafer',
        'city': 'Boulder',
        'state': 'CO'
    },
    {
        'name': 'Al Einstein',
        'city': 'Denver',
        'state': 'CO'
    },
    {
        'name': 'John Henry',
        'city': 'Hinton',
        'state': 'WV'
    },
    {
        'name': 'Randy Moss',
        'city': 'Rand',
        'state': 'WV'
    },
    {
        'name': 'Nicole K',
        'city': 'Asheville',
        'state': 'NC'
    },
    {
        'name': 'Jim Doe',
        'city': 'Charlotte',
        'state': 'NC'
    },
    {
        'name': 'Jane Taylor',
        'city': 'Faketown',
        'state': 'NC'
    }
]

person_group = itertools.groupby(people, get_state)

for key, group in person_group:
    print(key, len(list(group)))

---------------------------------

NY 2
CO 2
WV 2
NC 3

==========================================


Python - Coding Problem: Creating Your Own Iterators

** Coding Problem: Creating Your Own Iterators


==========================================

my_sentence = Sentence('This is a test')

for word in my_sentence:
print(word)


# This should have the following output:
# This
# is
# a
# test

---------------------------------

class Sentence:

def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()

def __iter__(self):
return self

def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]


my_sentence = Sentence('This is a test')

for word in my_sentence:
print(word)

---------------------------------

This
is
a
test

==========================================
==========================================

class Sentence:

def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()

def __iter__(self):
return self

def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]


my_sentence = Sentence('This is a test')

print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))
print(next(my_sentence))

---------------------------------

is
a
test
    print(next(my_sentence))
  File "C:\Users\purunet\Documents\py9\ii.py", line 14, in __next__
    raise StopIteration
StopIteration

---------------------------------

class Sentence:

def __init__(self, sentence):
self.sentence = sentence
self.index = 0
self.words = self.sentence.split()

def __iter__(self):
return self

def __next__(self):
if self.index >= len(self.words):
raise StopIteration
index = self.index
self.index += 1
return self.words[index]

def sentence(sentence):
for word in sentence.split():
yield word


my_sentence = sentence('This is a test')


for word in my_sentence:
print(word)

---------------------------------

This
is
a
test

==========================================