Skip to content

Commit eecd827

Browse files
committed
learning at 20190821 13:00
1 parent 007cb33 commit eecd827

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
from random import randint
3+
4+
data = [randint(0,20) for _ in range(30)]
5+
6+
d = dict.fromkeys(data,0)
7+
8+
for x in data:
9+
d[x] += 1
10+
11+
#sorted([(v,k) for k,v in d.items()], reverse=True)
12+
sorted(((v,k) for k,v in d.items()), reverse=True)
13+
14+
# 前3个
15+
sorted(((v,k) for k,v in d.items()), reverse=True)[:3]
16+
17+
import heapq
18+
19+
heapq.nlarest(3,((v,k) for k,v in d.items()))
20+
21+
22+
# solution2
23+
24+
from collections import Counter
25+
26+
c = Counter(data)
27+
c.most_common(3)
28+
29+
# 英文文章词频统计
30+
31+
import re
32+
33+
txt = open('example.txt').read()
34+
35+
# 使用正则将文本切割,使用非字母字符进行切割
36+
37+
word_list = re.split('\W+',txt)
38+
c2 = Counter(word_list)
39+
# 频度最高的前10个
40+
c2.most_common(10)
41+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from random import randint, simple
2+
sample('abcdefgh',randint(3,6))
3+
4+
# 第1轮
5+
6+
d1 = {k: randint(1,4) for k insample('abcdefgh',randint(3,6))}
7+
# 第2轮
8+
d2 = {k: randint(1,4) for k insample('abcdefgh',randint(3,6))}
9+
# 第3轮
10+
d3 = {k: randint(1,4) for k insample('abcdefgh',randint(3,6))}
11+
12+
# solution 1
13+
# find 公共键
14+
15+
[k for k in d1 if k in d2 and k in d3]
16+
17+
dl = [d1,d2,d3]
18+
19+
[for k in dl[0] if all(map(lambda d: k in d, dl[1:]))]
20+
21+
# solution 2
22+
23+
# python3 中reduce不是内置函数,在functools标准库中。Python2是内置函数,
24+
25+
from functools import reduce
26+
27+
reduce(lambda a, b: a & b ,map(dict.keys,dl))

python3_programming_tricks/cha02/2_datasture_tops.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,38 @@
6464

6565
sorted(d.items(), key=lambda item: item[1], reverse=True)
6666

67+
## 2.4 如何统计序列中元素的频度
68+
69+
**实际案例**
70+
71+
1 某随机序列中,找到出现次数最高的3个元素,他们出现的次数是多少
72+
2 对某英文文章的单词,进行词频统计,找到出现次数最高的10个单词,它们出现的次数是多少
73+
74+
**解决方案**
75+
76+
方案1:将序列转换为字典{元素:频度},根据字典中的值排序
77+
78+
这种方法不是很好,如果列表很大,需要将整个列表排序,而我们只需要最大前3个,这样显然是很浪费的
79+
在很大的列表中找到很小的前3个,通常会使用堆,python中也有实现,heapq
80+
81+
方案2:使用标准库collections中的Counter对象
82+
83+
## 2.5 如何快速找到多个字典的公共键key
84+
85+
**实际案例**
86+
87+
西班牙足球甲级联赛,每轮球员进球统计:
88+
第1轮:{'苏亚雷斯':1,'梅西':2,'本泽马':1,...}
89+
第2轮:{'苏亚雷斯':2,'C罗':1,'格里兹曼':2,...}
90+
第3轮:{'苏亚雷斯':1,'托雷斯','贝尔':1,...}
91+
.....
92+
93+
统计出前N轮,每场比赛都有进球的球员
94+
95+
**解决方案**
96+
97+
领用集合set的交集操作
98+
step1:使用字典的keys()方法,得到一个字典keys的集合
99+
step2:使用map函数,得到每个字典keys的集合
100+
step3:使用reduce函数,取所有字典的keys集合的交集
67101

0 commit comments

Comments
 (0)