Skip to content

Commit 0d55ebb

Browse files
author
chengxianhuan
committed
2 parents d182f6c + 32427c5 commit 0d55ebb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+147
-3
lines changed

.DS_Store

-28 KB
Binary file not shown.

README.md

Lines changed: 7 additions & 3 deletions

day-039/.DS_Store

6 KB
Binary file not shown.

day-039/itertools-chain-demo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import itertools
2+
x = itertools.chain("abc", "xyz")
3+
print(list(x))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import itertools
2+
print(list(itertools.combinations("abc", r=2)))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import itertools
2+
print(list(itertools.combinations_with_replacement("abc", r=2)))

day-039/itertools-compress-demo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import itertools
2+
data = [81, 82, 84, 76, 64, 78]
3+
tf = [1,1,0,1,1,0]
4+
print(list(itertools.compress(data, tf)))

day-039/itertools-count-demo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import itertools
2+
x = itertools.count(1,2)
3+
for k in x:
4+
print(k, end=", ")

day-039/itertools-cycle-demo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import itertools
2+
x = itertools.cycle("XYZ")
3+
for k in x:
4+
print(k, end = ", ")

day-039/itertools-dropwhile-demo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import itertools
2+
x = itertools.dropwhile(lambda x: x < 5, [1,3,5,7,4,2,1])
3+
print(list(x))

day-039/itertools-filterfalse-demo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import itertools
2+
x = itertools.filterfalse(lambda x: x < 5, [1,3,5,7,4,2,1])
3+
print(list(x))

day-039/itertools-groupby-demo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import itertools
2+
def sortBy(score):
3+
if score > 80:
4+
return "A"
5+
elif score >= 60:
6+
return "B"
7+
else:
8+
return "C"
9+
10+
scores = [81, 82, 84, 76, 64, 78, 59, 44, 55, 89]
11+
#scores = sorted(scores, key=sortBy) # 将此行代码注释打开即可得到合理的结果
12+
for m, n in itertools.groupby(scores, key=sortBy):
13+
print(m, list(n))

day-039/itertools-islice-demo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import itertools
2+
print(list(itertools.islice('123456789', 2)))
3+
print(list(itertools.islice('123456789', 2, 4)))
4+
print(list(itertools.islice('123456789', 2, None)))
5+
print(list(itertools.islice('123456789', 0, None, 2)))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import itertools
2+
print(list(itertools.permutations("aba", r=2)))

day-039/itertools-product-demo.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import itertools
2+
print(list(itertools.product("ab", "12")))
3+
print(list(itertools.product("ab", "ab")))
4+
print(list(itertools.product("ab", repeat=2)))

day-039/itertools-repeat-demo.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import itertools
2+
#x = itertools.repeat("XYZ")
3+
x = itertools.repeat("XYZ", 3)
4+
for k in x:
5+
print(k, end = ", ")

day-039/itertools-starmap-demo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import itertools
2+
print(list(itertools.starmap(pow,[(2,10), (3,3)])))

day-039/itertools-takewhile-demo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import itertools
2+
x = itertools.takewhile(lambda x: x < 5, [1,3,5,7,4,2,1])
3+
print(list(x))

day-040/.DS_Store

6 KB
Binary file not shown.

day-040/statistics-demo.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import statistics
2+
3+
example_list = [1,2,3,4,5,6]
4+
5+
x = statistics.mean(example_list)
6+
print(x)
7+
8+
x = statistics.harmonic_mean(example_list)
9+
print(x)
10+
print(1/sum([1/1,1/2,1/3,1/4,1/5,1/6])*6)
11+
12+
x = statistics.median(example_list)
13+
print(x)
14+
15+
x = statistics.median_low(example_list)
16+
print(x)
17+
18+
x = statistics.median_high(example_list)
19+
print(x)
20+
21+
x = statistics.mode([1,1,2,3,4,3,3,3,3])
22+
print(x)
23+
24+
x = statistics.mode(["a","b","c","d","d","a","a",])
25+
print(x)
26+
27+
x = statistics.pstdev([2,2,2,6])
28+
print(x)
29+
30+
x = statistics.pvariance([2,2,2,6])
31+
print(x)
32+
33+
x = statistics.stdev([2,2,2,6])
34+
print(x)
35+
36+
x = statistics.variance([2,2,2,6])
37+
print(x)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

day043-statistics/statistics.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#计算平均数
2+
>>> statistics.mean([1, 2, 3, 4, 5])
3+
4+
>>> from fractions import Fraction as F
5+
>>> statistics.mean([F(4, 7), F(4, 21), F(5, 4), F(1, 4)])
6+
7+
>>> from decimal import Decimal as D
8+
>>> statistics.mean([D("0.5"), D("0.78"), D("0.88"), D("0.988")])
9+
10+
11+
#计算调和平均数
12+
>>> statistics.harmonic_mean([4, 5, 7])
13+
14+
15+
#计算中值
16+
>>> statistics.median([1, 4, 7])
17+
18+
>>> statistics.median([1, 4, 7, 10])
19+
20+
21+
#计算中小值
22+
>>> statistics.median_low([1, 4, 7])
23+
24+
>>> statistics.median_low([1, 4, 7, 10])
25+
26+
27+
#计算中大值
28+
>>> statistics.median_high([1, 4, 7])
29+
30+
>>> statistics.median_high([1, 4, 7, 10])
31+
32+
33+
#计算中位数
34+
>>> statistics.median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])
35+
36+
>>> statistics.median_grouped([3, 4, 4, 5, 6], interval=1)
37+
38+
>>> statistics.median_grouped([1, 3, 5, 5, 7], interval=2)
39+
40+
41+
#计算众数
42+
>>> statistics.mode([1, 1, 2, 3, 3, 3, 3, 4])
43+
44+
>>> statistics.mode(["red", "blue", "blue", "blue", "green", "green", "red"])

0 commit comments

Comments
 (0)