192. Word Frequency

192. Word Frequency

 
QuestionEditorial Solution
  My Submissions
 
  • Total Accepted: 5272
  • Total Submissions: 20228
  • Difficulty: Medium

 

Write a bash script to calculate the frequency of each word in a text file words.txt.

For simplicity sake, you may assume:

  • words.txt contains only lowercase characters and space ' ' characters.
  • Each word must consist of lowercase characters only.
  • Words are separated by one or more whitespace characters.

 

For example, assume that words.txt has the following content:

the day is sunny the the
the sunny is is
Your script should output the following, sorted by descending frequency:
the 4
is 3
sunny 2
day 1

Note:
Don't worry about handling ties, it is guaranteed that each word's frequency count is unique.

思路:统计所有词放入llist,通过集合去重,根据set里面的关键字查每个单词频率

注意:读取txt文件行尾要去除/n

   myset=setlist(    mylist = list(set)

  list.count(optt统计list中opt对象的重复次数

 
import os
list1 = []
f = open('words.txt','r')
lines = f.readlines()
for eachline in lines:
    list1 += eachline.split()

list2 = list(set(list1))

dict = {}
for i in range(len(list2)):
    word = list2[i]
    num = list1.count(word)
    dict[word] = num
print dict

 

 

列表类型内建函数 List Method Operation

list.append(obj) 向列表中添加一个对象 obj

list.count(obj) 返回一个对象 obj 在列表中出现的次数

list.extend(seq)a 把序列 seq 的内容添加到列表中

list.index(obj, i=0, j=len(list)) 返回 list[k] == obj 的 k 值,并且 k 的范围在 i<=k<j;否则 引发 ValueError 异常.

list.insert(index, obj) 在索引量为 index 的位置插入对象 obj.

list.pop(index=-1)a 删除并返回指定位置的对象,默认是最后一个对象

list.remove(obj) 从列表中删除对象 obj

list.reverse() 原地翻转列表

list.sort(func=None,key=None,reverse=False)以指定的方式排序列表中的成员,如果 func 和 key 参数指定, 则按照指定的方式比较各个元素,如果 reverse 标志被置为 True,则列表以反序排列.

 

myset = set(list)

mylist = list(set)

 

创建set

 

>>> s1 = set ( "qiwsir" ) #把str中的字符拆解开,形成set.特别注意观察:qiwsir中有两个i
>>> s1         #但是在s1中,只有一个i,也就是不能重复
set ([ 'q' , 'i' , 's' , 'r' , 'w' ])
 
>>> s2 = set ([ 123 , "google" , "face" , "book" , "facebook" , "book" ])  #通过list创建set.不能有重复,元素可以是int/str
>>> s2
set ([ 'facebook' , 123 , 'google' , 'book' , 'face' ])        #元素顺序排列不是按照指定顺序
 
>>> s3 = { "facebook" , 123 }    #通过{}直接创建
>>> s3
)

 

s3 = {"facebook",123}    #通过{}直接创建

转载于:https://www.cnblogs.com/lovely7/p/5864684.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值