Skip to content

Commit

Permalink
Itertools Code Snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
CoreyMSchafer committed Nov 13, 2018
1 parent 588f067 commit 2259d6c
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
65 changes: 65 additions & 0 deletions Python/Itertools/iter-demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

import itertools


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


people = [
{
'name': 'John Doe',
'city': 'Gotham',
'state': 'NY'
},
{
'name': 'Jane Doe',
'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)

copy1, copy2 = itertools.tee(person_group)

for key, group in person_group:
print(key, len(list(group)))
# for person in group:
# print(person)
# print()
54 changes: 54 additions & 0 deletions Python/Itertools/snippets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

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



people = [
{
'name': 'John Doe',
'city': 'Gotham',
'state': 'NY'
},
{
'name': 'Jane Doe',
'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'
}
]
8 changes: 8 additions & 0 deletions Python/Itertools/test.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Date: 2018-11-08
Author: Corey
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?

0 comments on commit 2259d6c

Please sign in to comment.