Skip to content

Commit 2259d6c

Browse files
committed
Itertools Code Snippets
1 parent 588f067 commit 2259d6c

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

Diff for: Python/Itertools/iter-demo.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
import itertools
3+
4+
5+
def get_state(person):
6+
return person['state']
7+
8+
9+
people = [
10+
{
11+
'name': 'John Doe',
12+
'city': 'Gotham',
13+
'state': 'NY'
14+
},
15+
{
16+
'name': 'Jane Doe',
17+
'city': 'Kings Landing',
18+
'state': 'NY'
19+
},
20+
{
21+
'name': 'Corey Schafer',
22+
'city': 'Boulder',
23+
'state': 'CO'
24+
},
25+
{
26+
'name': 'Al Einstein',
27+
'city': 'Denver',
28+
'state': 'CO'
29+
},
30+
{
31+
'name': 'John Henry',
32+
'city': 'Hinton',
33+
'state': 'WV'
34+
},
35+
{
36+
'name': 'Randy Moss',
37+
'city': 'Rand',
38+
'state': 'WV'
39+
},
40+
{
41+
'name': 'Nicole K',
42+
'city': 'Asheville',
43+
'state': 'NC'
44+
},
45+
{
46+
'name': 'Jim Doe',
47+
'city': 'Charlotte',
48+
'state': 'NC'
49+
},
50+
{
51+
'name': 'Jane Taylor',
52+
'city': 'Faketown',
53+
'state': 'NC'
54+
}
55+
]
56+
57+
person_group = itertools.groupby(people, get_state)
58+
59+
copy1, copy2 = itertools.tee(person_group)
60+
61+
for key, group in person_group:
62+
print(key, len(list(group)))
63+
# for person in group:
64+
# print(person)
65+
# print()

Diff for: Python/Itertools/snippets.txt

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
letters = ['a', 'b', 'c', 'd']
3+
numbers = [0, 1, 2, 3]
4+
names = ['Corey', 'Nicole']
5+
6+
7+
8+
people = [
9+
{
10+
'name': 'John Doe',
11+
'city': 'Gotham',
12+
'state': 'NY'
13+
},
14+
{
15+
'name': 'Jane Doe',
16+
'city': 'Kings Landing',
17+
'state': 'NY'
18+
},
19+
{
20+
'name': 'Corey Schafer',
21+
'city': 'Boulder',
22+
'state': 'CO'
23+
},
24+
{
25+
'name': 'Al Einstein',
26+
'city': 'Denver',
27+
'state': 'CO'
28+
},
29+
{
30+
'name': 'John Henry',
31+
'city': 'Hinton',
32+
'state': 'WV'
33+
},
34+
{
35+
'name': 'Randy Moss',
36+
'city': 'Rand',
37+
'state': 'WV'
38+
},
39+
{
40+
'name': 'Nicole K',
41+
'city': 'Asheville',
42+
'state': 'NC'
43+
},
44+
{
45+
'name': 'Jim Doe',
46+
'city': 'Charlotte',
47+
'state': 'NC'
48+
},
49+
{
50+
'name': 'Jane Taylor',
51+
'city': 'Faketown',
52+
'state': 'NC'
53+
}
54+
]

Diff for: Python/Itertools/test.log

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Date: 2018-11-08
2+
Author: Corey
3+
Description: This is a sample log file
4+
5+
Okay, so this is a sample entry.
6+
I'm going to write a few more lines here.
7+
For the sake of this video, let's pretend this log file is thousands and thousands of lines... okay?
8+

0 commit comments

Comments
 (0)