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}    #通过{}直接创建

时间: 2024-11-29 09:19:03

192. Word Frequency的相关文章

[leetcode shell]192. Word Frequency

统计words.txt中每个单词出现的次数并排序 解法1: cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2,$1}' 解法2: cat words.txt | awk '{for(i=1;i<=NF;i++){count[$i]++}}END{for (i in count){print i,count[i]}}' | sort -k2nr

Word frequency analysis

Write a program that reads a file, breaks each line into words, scripts whitespace and punctuation from the words, and converts them to lowercase. Modify the program to print the 20 most frequently-used words in the book. First I downloaded the e-boo

[Bash]LeetCode192..统计词频 | Word Frequency

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. Wor

Word Frequency

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. Wor

[CareerCup] 17.9 Word Frequency in a Book 书中单词频率

17.9 Design a method to find the frequency of occurrences of any given word in a book. 这道题让我们找书中单词出现的频率,那么首先需要搞清楚的问题是,只需要统计一个单词,还是多个单词.如果是一个单词的话,那直接就遍历所有单词直接统计即可,如果是多个,就需要建立哈希表来建立每个单词和其出现次数之间的映射,然后再来查找即可,参见代码如下: unordered_map<string, int> make_dicti

[LeetCode] Word Frequency 单词频率

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. Wor

Individual Project - Word frequency program by HJB

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Text.RegularExpressions;using System.Threading.Tasks; using System.Collections;namespace ConsoleApplication1{ class v { public int n { get;

Individual Project - Word frequency program

1.做这个项目之前,因为之前在OO课中做过一些项目,这钟算法也非常熟悉,因此算上单纯的词法算法,和C#语言中文件操作的学习,预计一天之内应该可以写好.2.实际上做起来时,我发现c#与之前学过的java还是有些差别的,算法非常简单,但是学习使用c#花了许多时间,零零碎碎共做了2天. 3.原本一直认为程序的最大资源使用会是单词按词频排序,但是经过算法的分析,还是单词+空格+单词这种格式的判断比较耗费时间 我的算法是这样的:读入一个文本文件的所有字符,以一个字符串形式储存.从头到尾遍历字符串,认为大小

SoftwareEngineering Individual Project - Word frequency program

说实话前面c#实在没怎么学过.这次写起来感觉非常陌生,就连怎么引用名空间都忘记了.在经过恶补后还是慢慢地适应了. 1.项目预计用时: 构建并写出大概的数据结构,程序框架及模块: 30min 实现文件夹递归方问方法 :30min 实现从文件中读出符合要求的单词并统计 :2-3h 实现对单词的排序 : 1h 输出:10min 细节修改及错误排查:2-3h 程序优化: 1h 2.项目的实际用时: 构建并写出大概的数据结构,程序框架及模块: 30min 实现文件夹递归方问方法 :30min 实现从文件中