Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
105 changes: 105 additions & 0 deletions homeworks/B21562/final-homework/homework01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-
import codecs #引入 codecs 类
import os #引入 os 类
import collections


#1. 读取文件
def read_file(file_path):
f = codecs.open(file_path, 'r', "utf-8") # 打开路径里的文件
lines = f.readlines() #按行来读取
word_list=[] #要能够在主程序里使用刚刚读取并分割的单词,不能直接打印出来,而是应该把它存储到一个list变量里
for line in lines: #对每一行做循环
line = line.strip() #去掉每一行两侧的空格
words = line.split(" ") #并按照空格来区分单词
words=word_split(words) #按照连接符再分一次单词
# print words
word_list.extend(words)#把区分出来的单词加入到word_list中,注意,这里写append会报错,因为生成的words是多个单词组成的列表,应该使用extend
return word_list
def word_split(words): #再用其他方法分割单词
new_list=[]
for word in words:
if '-' not in word:
new_list.append(word)
else:
lst=word.split('-') #将用连接符链接的单词区分开
new_list.extend(lst)
return new_list
#2.获取格式化之后的单词
def format_word(word): #格式化一个单词
fmt = 'abcdefghijklmnopqrstuvwxyz-'
for char in word: #对于单词中的每一个字符
if char not in fmt: #如果它不在fmt字符串中,即不是小写的英文字母
word = word.replace(char, '') #就把单词中的这个字符替换为空格
return word.lower() #最后把单词中的所有字符换成小写

def format_words(words): #通过循环命令,来格式化所有的单词,并且还需要把空单词删掉
#format_word_list=[]
word_list=[]
for word in words: #对words列表中的所有单词
#format_word(word) #全部格式化
#format_word_list.append(word) #格式化之后加入到format_word_list列表中,可以简化为format_word_list.append(format_word(word))
wd=format_word(word)#在这里先把之前格式化的每一个单词赋值给wd
if wd: #再判断wd是不是一个空单词
word_list.append(format_word(word)) #如果不为空,才把它加入到列表里,这时候,列表里就没有空单词了
return word_list

#3.统计单词数目
#使用字典来统计单词{'aa':1,"bb":5}
def statictcs_words(words):
s_word_dict={}
for word in words:
if s_word_dict.has_key(word): #当字典里有了这个单词,第二次再遇到时,就对键值+1
s_word_dict[word]=s_word_dict[word]+1 #通过方括号来访问字典里每一个键的值,[word]表示键,冒号前面的部分,s_word_dict[word]表示值,冒号后的数字
else:
s_word_dict[word]=1 #一开始是一个空字典,第一次遇到某个单词时,运行这一句,为这个单词表示的键赋值为1

return s_word_dict

#4.对统计后的单词排序
def sort(s_word_dict):
from collections import OrderedDict #这里需要使用一个collections模块,用来生成有序字典
word_sort_dict=OrderedDict(sorted(s_word_dict.items(), key=lambda x: x[1],reverse=True))
#sorted命令用来给字典排序,但排序之后输出的结果不是字典,而是列表
#于是加上一个命令,把生成的列表变成一个有序字典
#for k,v in word_sort_dict.items():
# print k,v
return word_sort_dict

#4.输出字典文件到csv
def print_to_csv(volcaulay_map, to_file_path):
nfile = open(to_file_path,'w+')
for key in volcaulay_map.keys():
val = volcaulay_map[key]
nfile.write("%s,%s\n" % (key, str(val)))
nfile.close()
#5.输出排序结果到csv
def printsort_to_csv(volcaulay_map, to_file_path):
nfile = open(to_file_path,'w+')
for index,number in volcaulay_map.keys():
val = volcaulay_map[key]
nfile.write("%s,%s\n" % (key, str(val)))
nfile.close()

def main():
#1. read file

#word_list=read_file('data1/dt01.txt') #必须要加上这句话,传递了word_list之后,才能正确地打印
#print word_list #虽然前面return了word_list,但是在这里直接打印word_list还是会报错,原因是没有传递过来
words=read_file('data1/dt01.txt') #也可以给word_list改名words后传递过来,和上面是一样的
print words
print "获取了未格式化的单词%d个"%(len(words))
#2. format word
fwords=format_words(words)
print "获取了已经格式化的单词%d个" %(len(fwords))
print fwords
#3. 统计文件
s_word_dict=statictcs_words(fwords)
print s_word_dict
sortdict=sort(s_word_dict)
#4.输出文件
print_to_csv(sortdict, 'output/output01.csv')#输出文件这里,括号里逗号前是需要输出的结果变量,括号后是输出的路径


if __name__ == '__main__':
main()
116 changes: 116 additions & 0 deletions homeworks/B21562/final-homework/homework02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# -*- coding: utf-8 -*-
import codecs #引入 codecs 类
import os #引入 os 类
import collections #引入collections类

#1. 读取文件
def read_files(file_paths):
final_words=[]
for path in file_paths:
final_words.extend(read_file(path)) #读取其中的每一个文件,按read_file里的步骤分割后,加入到final_words列表中,注意没有等号的!
return final_words

#3.读取文件夹里的所有文件
def get_file_from_folder(folder_path):
file_paths=[]
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
file_paths.append(file_path) #这里记得不要写等号哦
return file_paths

def read_file(file_path):
f = codecs.open(file_path, 'r', "utf-8") # 打开路径里的文件
lines = f.readlines() #按行来读取
word_list=[] #要能够在主程序里使用刚刚读取并分割的单词,不能直接打印出来,而是应该把它存储到一个list变量里
for line in lines: #对每一行做循环
line = line.strip() #去掉每一行两侧的空格
words = line.split(" ") #并按照空格来区分单词
words=word_split(words) #按照连接符再分一次单词
# print words
word_list.extend(words)#把区分出来的单词加入到word_list中,注意,这里写append会报错,因为生成的words是多个单词组成的列表,应该使用extend,注意不要写等号
return word_list
def word_split(words): #再用其他方法分割单词
new_list=[]
for word in words:
if '-' not in word:
new_list.append(word)
else:
lst=word.split('-') #将用连接符链接的单词区分开
new_list.extend(lst)
return new_list
#2.获取格式化之后的单词
def format_word(word): #格式化一个单词
fmt = 'abcdefghijklmnopqrstuvwxyz-'
for char in word: #对于单词中的每一个字符
if char not in fmt: #如果它不在fmt字符串中,即不是小写的英文字母
word = word.replace(char, '') #就把单词中的这个字符替换为空格
return word.lower() #最后把单词中的所有字符换成小写

def format_words(words): #通过循环命令,来格式化所有的单词,并且还需要把空单词删掉
#format_word_list=[]
word_list=[]
for word in words: #对words列表中的所有单词
#format_word(word) #全部格式化
#format_word_list.append(word) #格式化之后加入到format_word_list列表中,可以简化为format_word_list.append(format_word(word))
wd=format_word(word)#在这里先把之前格式化的每一个单词赋值给wd
if wd: #再判断wd是不是一个空单词
word_list.append(format_word(word)) #如果不为空,才把它加入到列表里,这时候,列表里就没有空单词了
return word_list

#3.统计单词数目
#使用字典来统计单词{'aa':1,"bb":5}
def statictcs_words(words):
s_word_dict={}
for word in words:
if s_word_dict.has_key(word): #当字典里有了这个单词,第二次再遇到时,就对键值+1
s_word_dict[word]=s_word_dict[word]+1 #通过方括号来访问字典里每一个键的值,[word]表示键,冒号前面的部分,s_word_dict[word]表示值,冒号后的数字
else:
s_word_dict[word]=1 #一开始是一个空字典,第一次遇到某个单词时,运行这一句,为这个单词表示的键赋值为1
#s_word_dict= sorted(s_word_dict.iteritems(), key=lambda d:d[1], reverse = True)
#这句不能直接用在这里,否则生成的是一个列表,没有办法被后面的输出到csv模块调用
return s_word_dict

#4.对统计后的单词排序
def sort(s_word_dict):
from collections import OrderedDict #这里需要使用一个collections模块,用来生成有序字典
word_sort_dict=OrderedDict(sorted(s_word_dict.items(), key=lambda x: x[1],reverse=True))
#sorted命令用来给字典排序,但排序之后输出的结果不是字典,而是列表
#于是加上一个命令,把生成的列表变成一个有序字典
#for k,v in word_sort_dict.items():
# print k,v
return word_sort_dict

#5.输出文件到csv
def print_to_csv(volcaulay_map, to_file_path):
nfile = open(to_file_path,'w+')
for key in volcaulay_map.keys():
val = volcaulay_map[key]
nfile.write("%s,%s\n" % (key, str(val)))
nfile.close()
def main():
#1. read file

#word_list=read_file('data1/dt01.txt') #必须要加上这句话,传递了word_list之后,才能正确地打印
#print word_list #虽然前面return了word_list,但是在这里直接打印word_list还是会报错,原因是没有传递过来
#words=read_files(['data2/2016.01.02.txt','data2/2016.01.09.txt','data2/2016.01.16']) #这里如果只输入文件夹的路径会报错,因为只能输入一组文件列表
paths=get_file_from_folder('data2')
#print paths
words=read_files(paths)
#print paths
print "获取了未格式化的单词%d个"%(len(words))
# #2. format word
fwords=format_words(words)
print "获取了已经格式化的单词%d个" %(len(fwords))
#print fwords
# # #3. 统计文件
s_word_dict=statictcs_words(fwords)
#print s_word_dict
#4.排序
sortdict=sort(s_word_dict)
# # #5.输出文件
print_to_csv(sortdict, 'output/output02.csv')#输出文件这里,括号里逗号前是需要输出的结果变量,括号后是输出的路径


if __name__ == '__main__':
main()
40 changes: 40 additions & 0 deletions homeworks/B21562/homework-14item/homework01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/python
# -*- coding:utf-8 -*-

# 5. 加分题[选做]:修改程序,小贩的价格每低 1 元,老妈会多买一斤,最多买 4 斤,用程序表达出来
def main():
who = "小狼"
mprice = 1 #小贩的价格每低1元
mbuy = 1 #小狼多买1斤
maxbuy = 4 #最多买4斤
good_price = 4 #小贩的价格
buy_amount = 5-good_price #购买的数量
running = True

# 开始我的表演
# go 我们来走一组
print "小贩的价格每低%d元,%s多买%d斤,最多买%d斤"%(mprice,who,mbuy,maxbuy)

if good_price <= 4:
print '小贩的价格为%d元,买了%d斤'%(good_price,buy_amount)

else:
print '贵了'
print '不买'
while running:
realprice=good_price-1
good_price=realprice
realbuy=buy_amount+1
buy_amount=realbuy
if realbuy>4:
print "最多只买4斤"
running=False
else:
print "小贩的价格为%d元,买了%d斤"%(realprice,realbuy)

#homework
#1. 看 day1-homework.py

#run function
if __name__ == '__main__':
main()
85 changes: 85 additions & 0 deletions homeworks/B21562/homework-14item/homework02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @author: Guoshushu

# Day2 - 为了大家能够做好这一次的第一个作业
# 继续深化变量的练习
#
# homework2

def main():
#01.int
total_apple_price = 100
apple_number = 30.0 #这里如果只输入整数的话,除法计算出的结果也只以整数来显示
pie_number = 53
pie_price = 2.98

#02. * /
apple_price = total_apple_price / apple_number
pie_total_price = pie_number * pie_price

#03. try to explain what's float
print '每个苹果价值 %d 元' %(apple_price) #d表示计算结果取整数
print '每个苹果价值 %g 元' %(apple_price) #g表示计算结果取小数,默认显示6位有效数字,无意义的0不显示
print '每个苹果价值 %f 元' %(apple_price) #f表示计算结果取小数,默认显示小数点后6位,无意义的0同样显示
print 'pie cost %d ' % (pie_total_price)
print 'pie cost %0.2g ' % (pie_total_price) #写在g前面的0.2,表示计算结果只显示2位有效数字,采用科学计数法来显示结果
print 'pie cost %0.2f ' % (pie_total_price) #写在f前面的0.2,表示计算结果显示小数点后2位

#04. **
number = 2**0.5 #一个乘号表示相乘关系,两个乘号表示幂
print number
print 'number = %0.3g' % (number)

#05. what else?
# 在 python简明教程中找到第 34 页,然后搞懂所有的符号~
# 每个符号在限免测试一下 除了 << >> ^ ~ 几个不用理解,之后会讲
# 不用理解优先级,只用记住一句:括号里面的最先计算
#如:
print 'test: %d' % (1 != 2)
if 1:
print 'good'
print 'test: %d' % (1 >= 2)
if 0:
print 'bad'
if 1:
print "why"
print (1>=2)
if(2 != 2):
print 'wweewe we w'
else:
print "ok"
#请开始你的表演
print "a+b= %s" %('a'+'b')
print "3.6+1.7=%d" %(3.6+1.7)
print "3.6+1.7=%g" %(3.6+1.7)
print "3.6+1.7=%0.2f" %(3.6+1.7)
print -5.3
print 2**(1/2.0)
print 'la'*3
print 4/3
print 4.0/3
print 4//3.0
print 5%2
print 5<3
print "%d"%(5<3)
x=3
y=3
print "%d"%(x==y)
z=5
print "x==z?%d"%(x==z)
print x!=z
print x>=z
print x<z
x=True
y=False
z=1
print (x and y)
print (x and z)
print (x or y)
print (y or z)
print 1+5*6**2-3.0/3*(8-2)
print 1+5*(6**2)-3.0/3*(8-2)
print 1+(5*6)**2-3.0/3*(8-2)
if __name__ == '__main__':
main()
Loading