Python语言基础--4(词频统计)
2018-01-23 11:05
363 查看
#coding:utf-8
import time
import string
num=[6,2,7,4,1,3,5]
str='dfjyfhbs'
print sorted(num,reverse=True)
for a,b in zip(num,str):
print b,'is',a
a=[]
t1=time.clock()
for i in range(1,20000):
a.append(i)
print time.clock()-t1
t1=time.clock()
b=[i for i in range(1,200)]
#print b
print time.clock()-t1
#列表推导式 线'|'后面是for循环的表达式,而线'|'前面的可以认为是我们想要放在列表中的元素
#list=[item|for item in iterable]
c=[n for n in range(1,10) if n%2==0]
z=[letter.lower() for letter in 'ABCDEFG']
#c[2, 4, 6, 8]
#z['a', 'b', 'c', 'd', 'e', 'f', 'g']
#print c,'\n',z
#词频统计
path='C:\Users\Administrator\Desktop\s.txt '
with open(path,'r')as text:
#strip(string.punctuation)可以去掉所有的标点符号
#在文字的首位去掉了连在一起的标点符号,并把首字母大写的单词转化成小写
words=[raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]
#将列表用set函数转换成集合,自动去掉了其中所有重复的元素
words_index=set(words)
#创建一个以单词为key,出现频率为value的字典
counts_dict={index:words.count(index) for index in words_index}
print(words)
#打印整理后的函数,其中key=lambda x:counts_dict[x]叫做lambda表达式
#可以暂且理解为以字典中的值为排序的参数
for word in sorted(counts_dict,key=lambda x:counts_dict[x],reverse=True):
print('{}---{} times'.format(word,words.count(word)))
import time
import string
num=[6,2,7,4,1,3,5]
str='dfjyfhbs'
print sorted(num,reverse=True)
for a,b in zip(num,str):
print b,'is',a
a=[]
t1=time.clock()
for i in range(1,20000):
a.append(i)
print time.clock()-t1
t1=time.clock()
b=[i for i in range(1,200)]
#print b
print time.clock()-t1
#列表推导式 线'|'后面是for循环的表达式,而线'|'前面的可以认为是我们想要放在列表中的元素
#list=[item|for item in iterable]
c=[n for n in range(1,10) if n%2==0]
z=[letter.lower() for letter in 'ABCDEFG']
#c[2, 4, 6, 8]
#z['a', 'b', 'c', 'd', 'e', 'f', 'g']
#print c,'\n',z
#词频统计
path='C:\Users\Administrator\Desktop\s.txt '
with open(path,'r')as text:
#strip(string.punctuation)可以去掉所有的标点符号
#在文字的首位去掉了连在一起的标点符号,并把首字母大写的单词转化成小写
words=[raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]
#将列表用set函数转换成集合,自动去掉了其中所有重复的元素
words_index=set(words)
#创建一个以单词为key,出现频率为value的字典
counts_dict={index:words.count(index) for index in words_index}
print(words)
#打印整理后的函数,其中key=lambda x:counts_dict[x]叫做lambda表达式
#可以暂且理解为以字典中的值为排序的参数
for word in sorted(counts_dict,key=lambda x:counts_dict[x],reverse=True):
print('{}---{} times'.format(word,words.count(word)))
相关文章推荐
- (改进)Python语言实现词频统计
- 【Python基础】1.2.1 字典统计词频
- Python基础知识(3)——中英文文档词频统计
- WordsCount(词频统计)-Python语言编写!
- 【脚本语言系列】关于Python基础知识函数缓存,你需要知道的事
- Python 对文本先按词频统计,若相同按字典排序,后取TopN
- Python由词频统计改编的数据分组汇总
- 程序语言 -- Python语法基础
- Python语言基础(三)_字符串和编码
- 程序设计基础(Python语言)”编程练习(1)
- Python游戏:语言基础和技术
- 坚持#第227天~零基础自学云计算基础语言应用之python6~7节and1~3节
- Python语言基础
- python基础整理——计算机语言分类
- python 编程语言基础技术框架
- python语言学习6——python基础
- 零基础掌握百度地图兴趣点获取POI爬虫(python语言爬取)(进阶篇)
- 【脚本语言系列】关于Python基础知识协程,你需要知道的事
- 【脚本语言系列】关于Python基础知识对象变动,你知道的事
- 如何利用python统计英文文章词频