首页 > 代码库 > 192. Word Frequency

192. Word Frequency

192. Word Frequency

 
 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 thethe sunny is is
Your script should output the following, sorted by descending frequency:
the 4is 3sunny 2day 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 oslist1 = []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] = numprint 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}    #通过{}直接创建

192. Word Frequency