Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
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
226df7b
Create readme.md
liupengyuan Nov 6, 2017
98e76e0
Update readme.md
liupengyuan Nov 6, 2017
9b8ebb6
Update readme.md
liupengyuan Nov 6, 2017
b1814f6
Create namelist.md
liupengyuan Nov 6, 2017
856d1ed
Update readme.md
liupengyuan Nov 6, 2017
a48f6dc
Update readme.md
liupengyuan Nov 6, 2017
f29ed27
Add files via upload
liupengyuan Nov 13, 2017
c08faaa
Create readme.md
liupengyuan Nov 13, 2017
a40c76b
Add files via upload
liupengyuan Nov 13, 2017
eec9e63
Add files via upload
liupengyuan Nov 13, 2017
2301fee
Update 8.md
liupengyuan Nov 14, 2017
d7df626
Update 8.md
liupengyuan Nov 14, 2017
57d9a7e
Update 6.md
liupengyuan Nov 14, 2017
bc274b8
Update 6.md
liupengyuan Nov 14, 2017
cfd3ba8
Update 6.md
liupengyuan Nov 14, 2017
43723be
Update 6.md
liupengyuan Nov 14, 2017
4cc7666
Create 1
zhushucheng Nov 15, 2017
2a0160a
Add files via upload
liupengyuan Nov 16, 2017
a084777
Merge pull request #621 from zhushucheng/master
liupengyuan Nov 17, 2017
950acab
Update 9.md
liupengyuan Nov 20, 2017
0975f96
Update 9.md
liupengyuan Nov 20, 2017
bb1d38c
Update 7.md
liupengyuan Nov 20, 2017
6027106
Update 6.md
liupengyuan Nov 20, 2017
8cf1d30
Add files via upload
liupengyuan Nov 23, 2017
3c9def6
Update python爬虫入门.ipynb
liupengyuan Nov 23, 2017
1fb421d
Update 7.md
liupengyuan Nov 23, 2017
9fccec3
Add files via upload
liupengyuan Nov 25, 2017
427b288
Add files via upload
liupengyuan Nov 25, 2017
a9327ad
Create readme.md
liupengyuan Nov 25, 2017
b3d24b4
Add files via upload
liupengyuan Nov 25, 2017
569d28d
Add files via upload
liupengyuan Nov 25, 2017
a509377
Add files via upload
liupengyuan Nov 25, 2017
3345006
Add files via upload
liupengyuan Nov 25, 2017
5c0b213
Add files via upload
liupengyuan Nov 25, 2017
e4afac8
Delete python正则表达式基础快速教程.ipynb
liupengyuan Nov 27, 2017
7b11d7c
Create 基于电影评分、类型的可视化分析
nino1123 Dec 10, 2017
e415b48
Delete 基于电影评分、类型的可视化分析
nino1123 Dec 11, 2017
8e013ac
项目立项
nino1123 Dec 11, 2017
e8a08bd
Delete 基于电影评分、类型的可视化分析
nino1123 Dec 11, 2017
2627002
Create 基于电影评分、类型的可视化分析
nino1123 Dec 11, 2017
67576fe
Delete 基于电影评分、类型的可视化分析
nino1123 Dec 11, 2017
ab0a2d7
Create 基于电影评分、类型的可视化分析
nino1123 Dec 11, 2017
687eae2
Delete 基于电影评分、类型的可视化分析
nino1123 Dec 11, 2017
963378e
项目立项
nino1123 Dec 11, 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
35 changes: 12 additions & 23 deletions chapter2/6.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,14 @@ while i < n:

```python
# 正序输出单词示例2,假设确定会输入5个单词
word1 = None
word2 = None
word3 = None
word4 = None
word5 = None

i = 0
while i < 5:
word = input('请输入一个单词,回车结束')
if word1 == None:
word1 = word
elif word2 == None:
word2 = word
elif word3 == None:
word3 = word
elif word4 == None:
word4 = word
else:
word5 = word
i += 1

word1 = input('请输入一个单词,回车结束')
word2 = input('请输入一个单词,回车结束')
word3 = input('请输入一个单词,回车结束')
word4 = input('请输入一个单词,回车结束')
word5 = input('请输入一个单词,回车结束')


print(word5)
print(word4)
Expand Down Expand Up @@ -157,6 +145,7 @@ while i < n:
words.append(word)
i += 1

i = len(words) #事实上,这句话也可以省略,读者可自行分析原因,但不建议省略,影响程序可读性
while i > 0:
i -= 1
print(words[i])
Expand Down Expand Up @@ -410,8 +399,8 @@ for i in range(500):
outside_xs.append(x)
outside_ys.append(y)
# 画点
p.circle(inside_x, inside_y, size=3, color = 'red') # circle为画圆函数,x,y为坐标,size为大小,color为颜色
p.circle(outside_x, outside_y, size=3, color = 'blue')
p.circle(inside_xs, inside_ys, size=3, color = 'red') # circle为画圆函数,x,y为坐标,size为大小,color为颜色
p.circle(outside_xs, outside_ys, size=3, color = 'blue')

# 显示结果
show(p)
Expand Down Expand Up @@ -680,8 +669,8 @@ for i in range(10):

6.8 习题
- 将前面几章用while循环的习题,用for循环实现,并尽量写成函数。
- 写函数,返回一个list中所有数字的和
- 写函数,返回一个list中的最小值
- 写函数,返回一个list中的最大值,最小值,平均值。(不用内置的求和求函数),以[1,2,-1,55,100,899,-10,3,12.5,5.8]为例。
- 写函数,返回某个元素/对象在一个list中的位置,如果不在,则返回-1.
- 写函数,可将两个相同长度的list,间隔插入,生成新的list。例如:给两个list,a=[1,2,3,4], b=[5,6,7,8]。则可以生成:[1,5,2,6,3,7,4,8]。
- 写函数,可求两个向量的夹角余弦值,向量可放在list中。主程序调用该函数。
- 挑战性习题:python语言老师为了激励学生学python,自费买了100个完全相同的Macbook Pro,分给三个班级,每个班级至少分5个,用穷举法计算共有多少种分法?
4 changes: 2 additions & 2 deletions chapter2/7.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ for i in range(x-1, -1, -1):
# 示例代码 9
line = '北京语言大学信息科学学院'
x = 4
print(line[0:x] + line[x-1:0:-1] + line[0])
print(line[0:x] + line[x-1::-1])   # print(line[0:x] + line[x-1:0:-1] + line[0])
```

本段代码中形如`序列[m:n:i]`的操作称为**序列切片**,就是从序列中取出索引在[m,n)之间,以i为间隔的所有对象,默认i为1。
Expand Down Expand Up @@ -260,7 +260,7 @@ print(numbers)
numbers = tuple()
print(numbers)

numbers = (1)
numbers = (1)   #数字而非元组
print(numbers)

numbers = (1,)
Expand Down
8 changes: 4 additions & 4 deletions chapter2/8.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def get_ch_table(line):
# 主程序
fh = open(r'd:\temp\idioms_correct.txt')
text = fh.read()
chs = get_ch_table(text.replace(r'\n', ''))
chs = get_ch_table(text.replace('\n', ''))

print(len(chs), chs)
```
Expand Down Expand Up @@ -278,7 +278,7 @@ idiom = '千钧一发' #假设抽取到了这个'成语'

fh = open(r'd:\temp\idioms_correct.txt')
text = fh.read()
chs = get_ch_table(text.replace(r'\n', ''))
chs = get_ch_table(text.replace('\n', ''))

guess_ch_table = [ch for ch in idiom]
while len(guess_ch_table) < 6:
Expand Down Expand Up @@ -319,7 +319,7 @@ poems = fh.read().split()
fh.close()

for guess in guesses:
if guess in poems:
if ''.join(guess) in poems:
print('答案是:' guess)
```

Expand Down Expand Up @@ -445,7 +445,7 @@ def idiom_robot(file_name):
text = fh.read()
idioms = text.split()
idiom = random.choice(idioms)
chs = get_ch_table(text.replace(r'\n', ''))
chs = get_ch_table(text.replace('\n', ''))

guess_ch_table = [ch for ch in idiom]
while len(guess_ch_table) < 6:
Expand Down
19 changes: 9 additions & 10 deletions chapter2/9.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

9.1 抽取指定行文本作为实验语料

请读者下载文本文件:`http://pan.baidu.com/s/1c1RukqW 密码: 3c32`,并将文件解压,保存到目录:`d:\temp\`下,文件名为:`语料.txt`。解压时间较长,请耐心等待(好在只解压一次)。  
请读者下载文本文件:`http://pan.baidu.com/s/1c1RukqW 密码: 3c32`,并将文件解压,保存到目录:`d:\temp\`下,文件名为:`语料.txt`。解压时间较长,请耐心等待(好在只解压一次)。   (下载慢的话,可以临时下载:http://yunpan.blcu.edu.cn:80/link/FC61CB2B791A1999439CEC52C1A30CE2,作为小的试验文件,只有100多k)。
可用文本编辑器打开`语料.txt`来查看文件内数据格式,但是打开这个10G文本文件比较慢。我们可以进入到powershell下,键入:`Get-Content d:\temp\语料.txt -totalcount 10`,来查看文件的前10行。
对单个文本文件,如果比较大(如10G bytes),则可以考虑先取这个文件的前n(如n=5000)行另存为一个小文件,对这个小文件来进行统计,如果没有问题了,再对这个较大的文件进行处理。
当然,在大多数时候,我们面临的统计任务可能会是很多文件,可能会是较大规模的语料(几十G或者更多),则也最好不要直接对其进行编程操作,而是拷贝几个相同格式的语料到一个临时目录,对这个目录进行统计实验,这样不但执行起来快捷,而且如果程序有错误,也容易查找。
Expand Down 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