Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
70f0bd7
Update 爬虫入门.md
liupengyuan Oct 1, 2017
de89146
Update 爬虫入门.md
liupengyuan Oct 1, 2017
bca921e
Update 爬虫入门.md
liupengyuan Oct 1, 2017
e0ef997
Update 爬虫入门.md
liupengyuan Oct 1, 2017
5b15bfc
Update 爬虫入门.md
liupengyuan Oct 1, 2017
82393e1
Update 爬虫入门.md
liupengyuan Oct 1, 2017
6f312fc
Update 爬虫入门.md
liupengyuan Oct 1, 2017
1f35bdc
Update 爬虫入门.md
liupengyuan Oct 1, 2017
0b15048
Create novel_site_list
liupengyuan Oct 3, 2017
4710b45
Rename novel_site_list to novel_site_list.md
liupengyuan Oct 3, 2017
f94cd58
Update novel_site_list.md
liupengyuan Oct 3, 2017
97bbbc5
Update 爬虫入门.md
liupengyuan Oct 4, 2017
300594c
Update 爬虫入门.md
liupengyuan Oct 4, 2017
0c41b2f
Update 爬虫入门.md
liupengyuan Oct 4, 2017
db46e0f
Update 爬虫入门.md
liupengyuan Oct 4, 2017
6d9860c
Update 爬虫入门.md
liupengyuan Oct 5, 2017
c7253c1
Delete task4.html
liupengyuan Oct 7, 2017
4f1c206
Add files via upload
liupengyuan Oct 8, 2017
3795204
Add files via upload
liupengyuan Oct 8, 2017
1481277
Add files via upload
liupengyuan Oct 8, 2017
9562209
Create Markdown Basic Tutorial.md
liupengyuan Oct 8, 2017
1042b73
Update Latex Equation Tutorial .ipynb
liupengyuan Oct 8, 2017
0c03da1
Update pandas Tutorial (Series).ipynb
liupengyuan Oct 8, 2017
469862a
Update pandas Tutorial (Series).ipynb
liupengyuan Oct 8, 2017
93b97c2
Update pandas Tutorial (Series).ipynb
liupengyuan Oct 8, 2017
598c2a8
Add files via upload
liupengyuan Oct 8, 2017
c7f4f4a
Delete pandas Tutorial (DataFrame).ipynb
liupengyuan Oct 14, 2017
3a0bb63
Add files via upload
liupengyuan Oct 14, 2017
efa3102
Delete pandas Tutorial (Series).ipynb
liupengyuan Oct 14, 2017
6850a90
Update 2.md
liupengyuan Oct 17, 2017
b69e558
Update 2.md
liupengyuan Oct 17, 2017
d6c638c
Update 9.md
liupengyuan Oct 20, 2017
e3e12cb
Update 9.md
liupengyuan Oct 20, 2017
1f4e7dc
Update task10.md
liupengyuan Oct 20, 2017
710d125
Update task10.md
liupengyuan Oct 21, 2017
113afd5
Add files via upload
liupengyuan Oct 21, 2017
1df3b56
Delete pandas+Tutorial+DataFrame.ipynb
liupengyuan Oct 21, 2017
c5d8ca5
Delete pandas+Tutorial+Series.ipynb
liupengyuan Oct 21, 2017
bcff494
Add files via upload
liupengyuan Oct 22, 2017
dba561a
Update 4.ipynb
liupengyuan Oct 24, 2017
c15bc82
Update 5.md
liupengyuan Oct 29, 2017
5e41118
Add files via upload
liupengyuan Nov 5, 2017
28fa6d0
Update and rename python正则表达式.ipynb to python正则表达式基础快速教程.ipynb
liupengyuan Nov 5, 2017
954c2e7
Update python正则表达式基础快速教程.ipynb
liupengyuan Nov 5, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chapter1/2.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ print('再见!', name)
```
- 练习2:仿照实践1,写出由用户指定整数个数,并由用户输入多个整数,并求和的代码。
- 练习3:用户可以输入的任意多个数字,直到用户不想输入为止。
- 练习4:用户可以输入的任意多个数字,直到输入所有数字的和比当前输入数字小,且输入所有数字的积比当前输入数字的平方大
- 练习4:用户可以输入的任意多个数字,直到: 输入所有数字的和比当前输入数字小,且输入所有数字的积大于500

[^1]:实际上有多种代码缩进方式,我们推荐并只介绍python创始人采用的这种方式。

Expand Down
2 changes: 1 addition & 1 deletion chapter2/4.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
"# 主程序\n",
"n=100\n",
"print('0:',example_0(n))\n",
"print('1:',example_0(n))\n",
"print('1:',example_1(n))\n",
"```\n",
"\n",
"没有`return`语句或者`return`语句后面没有参数,调用函数后函数的值都是`None`。因此,在程序不需要返回值的时候,可以不用return语句。 \n",
Expand Down
4 changes: 2 additions & 2 deletions chapter2/5.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def guess_game():
number = random.randint(1, n)
max_times = math.ceil(math.log2(n))
guess_times = 0
while guess_times <= max_times:
while guess_times < max_times:
guess = int(input('请输入你猜测的整数,回车结束。'))
guess_times += 1
print('一共可以猜', max_times, '次')
Expand Down Expand Up @@ -625,7 +625,7 @@ def guess_game():
max_times = math.ceil(math.log(n, 2))
guess_times = 0

while guess_times <= max_times:
while guess_times < max_times:
guess = int(input('请输入你猜测的整数,回车结束。'))
guess_times += 1
print('一共可以猜', max_times, '次')
Expand Down
17 changes: 8 additions & 9 deletions chapter2/9.md
Original file line number Diff line number Diff line change
Expand Up @@ -905,18 +905,17 @@ def count_words_freq_dict(filename):

with open(filename) as f:
for line in f:
words_freq_dict += Counter([word.split('/')[0] for word in line.split()])
words_freq_dict.update([word.split('/')[0] for word in line.split()])
return words_freq_dict
```

示例程序9-26中:

- 首先从`collections`模块中引入了`Counter`类型
- `words_freq_dict = Counter()`是建立了一个空的Counter对象,Counter类型类似于`dict`,也可以视为一种特殊的`dict`。
- `Counter([word.split('/')[0] for word in line.split()])`,实际上是将一个`list`转为`Counter`类型,转换过程中,`Counter()`会自动统计`list`中的对象频次,并以键值对的形式存入`Counter`对象,且与`dict`类似,这个`Counter`对象的`key`是可哈希的,即可利用哈希函数存取,时间复杂度为O(1)。
- `Counter`对象支持加法操作,其结果就是新的`Counter`对象中相同键的值相加,同时保留各自不同的键值对。
- `words_freq_dict.update([word.split('/')[0] for word in line.split()])`,其中`update()`是`Counter()`类型变量的一个函数,会自动更新其参数(一般为一个序列)中的对象频次,并以键值对的形式存入`Counter`对象,且与`dict`类似,这个`Counter`对象的`key`是可哈希的,即可利用哈希函数存取,时间复杂度为O(1)。

好吧,我们将词频统计的代码精简到了7行,且效率很高,这就是python语言的威力。
好吧,我们将词频统计的代码精简到了6行,且效率很高,这就是python语言的威力。
在本节任务中,我们利用词频统计任务,将程序的效率逐步提升,与此同时,所用数据类型逐步复杂,代码量逐步减少。
之所以这样安排而不是直接给出最优答案确实是期望在这个过程中,读者能对各种相关数据类型和算法有更清醒的认识和理解,也能够进一步锻炼使用python进行编程的能力。
通过1-9这几个任务,我们已经基本介绍并掌握了python语言的常用基本语法,并已对python编程有一定的实践能力。
Expand Down Expand Up @@ -1016,14 +1015,14 @@ print(palindrome(seq))
#coding: utf-8
#示例程序9-32

from collections import Counter
from collections import defaultdict

#假设之前已经词频统计完成,并已放入Counter或dict对象word_table中

ch_table = Counter()
ch_table = defaultdict(int)
for word, freq in word_table.items():
for ch in word:
ch_table += Counter({ch:freq})
ch_table[ch] += freq

for ch, freq in ch_table.items():
print(ch, freq, sep=':', end='|')
Expand All @@ -1032,7 +1031,7 @@ for ch, freq in ch_table.items():

示例程序9-32中:

- `Counter({ch:freq})`是利用一个词典来初始化一个Counter对象
- 对词频统计结果中的每个词进行遍历,对组成每个词的字进行遍历,每个词中包含的字的频次为当前的对应词的词频

9.9 拓展与汇总

Expand Down Expand Up @@ -1202,7 +1201,7 @@ def count_words_freq_dict(filename):

with open(filename) as f:
for line in f:
words_freq_dict += Counter([word.split('/')[0] for word in line.split()])
words_freq_dict.update([word.split('/')[0] for word in line.split()])
return words_freq_dict

def main():
Expand Down
Loading