作業四
阿新 • • 發佈:2018-09-27
pri set pan ems trac pen lambda lower 集合
#讀取文件 fo =open(‘bigworld.txt‘,‘r‘) bigg=fo.read().lower() fo.close() print(bigg) #大小寫 str.lower(bigg) #標點符號 strBig=bigg.replace(‘.‘,‘ ‘) #特殊符號 sep=‘.,;:?!-_‘ for ch in sep: strBig=strBig.replace(ch,‘ ‘) #字符串分隔 strList=strBig.split() print(len(strList),strList) #集合 strSet=set(strList) exclude={‘the‘,‘i‘} print(len(strSet),strSet) #字典 strDict={} for you in strSet: strDict[you]=strList.count(you) print(len(strDict),strDict) #列表 wcList=list(strDict.items()) print(wcList) wcList.sort(key=lambda x:x[1],reverse=True) print(wcList) #top 20 for i in range(20): print(wcList[i])
2.
classmates=[‘Tracy‘,‘Bod‘,‘Tracy‘,‘李三‘,‘Tracy‘] print(classmates) classmates.sort() print(classmates) score=[85,90,50,60,95] cs=dict(zip(classmates,score)) print(cs) csList=list(cs.items()) print(csList) def takeSecond(elem): x=elem[1] return x csList.sort(key=takeSecond,reverse=True) print(csList
3.
#英文小說 fo =open(‘blind.txt‘, ‘r‘) big=fo.read().lower() fo.close() print(big)
4.
#三國演義小說 import jieba fo =open(‘bigbig.txt‘,‘r‘,encoding=‘utf-8‘) biga=fo.read().lower() fo.close() print(biga) print(jieba.cut(biga)) print(list(jieba.cut(biga))) print(list(jieba.cut(biga,cut_all=True))) print(list(jieba.cut_for_search(biga)))
作業四