Repository navigation
Expand file tree
/
Copy pathch10_ex5.py
More file actions
91 lines (73 loc) · 2.64 KB
/
Copy pathch10_ex5.py
File metadata and controls
91 lines (73 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
"""Functional Python Programming
Chapter 10, Example Set 5
"""
from itertools import groupby
from collections import defaultdict
def partition( iterable, key=lambda x:x ):
"""Sort not required."""
pd = defaultdict(list)
for row in iterable:
pd[key(row)].append( row )
for k in sorted(pd):
yield k, iter(pd[k])
def partition_s( iterable, key= lambda x:x ):
"""Sort required"""
return groupby( iterable, key )
test_data = """
>>> data = [('4', 6.1), ('1', 4.0), ('2', 8.3), ('2', 6.5), ('1', 4.6),
... ('2', 6.8), ('3', 9.3), ('2', 7.8), ('2', 9.2), ('4', 5.6),
... ('3', 10.5), ('1', 5.8), ('4', 3.8), ('3', 8.1), ('3', 8.0),
... ('1', 6.9), ('3', 6.9), ('4', 6.2), ('1', 5.4), ('4', 5.8)]
>>> for group, group_iter in partition( list(data), key=lambda x:x[0] ):
... print( group, tuple(group_iter) )
1 (('1', 4.0), ('1', 4.6), ('1', 5.8), ('1', 6.9), ('1', 5.4))
2 (('2', 8.3), ('2', 6.5), ('2', 6.8), ('2', 7.8), ('2', 9.2))
3 (('3', 9.3), ('3', 10.5), ('3', 8.1), ('3', 8.0), ('3', 6.9))
4 (('4', 6.1), ('4', 5.6), ('4', 3.8), ('4', 6.2), ('4', 5.8))
>>> for group, group_iter in partition_s( sorted(data), key=lambda x:x[0] ):
... print( group, tuple(group_iter) )
1 (('1', 4.0), ('1', 4.6), ('1', 5.4), ('1', 5.8), ('1', 6.9))
2 (('2', 6.5), ('2', 6.8), ('2', 7.8), ('2', 8.3), ('2', 9.2))
3 (('3', 6.9), ('3', 8.0), ('3', 8.1), ('3', 9.3), ('3', 10.5))
4 (('4', 3.8), ('4', 5.6), ('4', 5.8), ('4', 6.1), ('4', 6.2))
"""
mean= lambda seq: sum(seq)/len(seq)
var= lambda mean, seq: sum( (x-mean)**2/mean for x in seq)
def summarize( key_iter ):
key, item_iter= key_iter
values= tuple((v for k,v in item_iter))
μ= mean(values)
return key, μ, var(μ, values)
test_summarize="""
>>> data = [('4', 6.1), ('1', 4.0), ('2', 8.3), ('2', 6.5), ('1', 4.6),
... ('2', 6.8), ('3', 9.3), ('2', 7.8), ('2', 9.2), ('4', 5.6),
... ('3', 10.5), ('1', 5.8), ('4', 3.8), ('3', 8.1), ('3', 8.0),
... ('1', 6.9), ('3', 6.9), ('4', 6.2), ('1', 5.4), ('4', 5.8)]
>>> partition1= partition( list(data), key=lambda x:x[0] )
>>> groups1= map( summarize, partition1 )
>>> for g, s, s2 in groups1:
... print( g, round(s,2), round(s2,2) )
1 5.34 0.93
2 7.72 0.63
3 8.56 0.89
4 5.5 0.7
>>> partition2= partition_s( sorted(data), key=lambda x:x[0] )
>>> groups2= map( summarize, partition2 )
>>> for g, s, s2 in groups2:
... print( g, round(s,2), round(s2,2) )
1 5.34 0.93
2 7.72 0.63
3 8.56 0.89
4 5.5 0.7
"""
__test__ = {
"test_data": test_data,
"test_summarize": test_summarize,
}
def test():
import doctest
doctest.testmod( verbose=1 )
if __name__ == "__main__":
test()
#performance()