192. Word Frequency

Source: Internet
Author: User

192. Word FrequencyQuestionEditorial SolutionMy Submissions
    • Total accepted:5272
    • Total submissions:20228
    • Difficulty:medium

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

For simplicity sake, your may assume:

    • words.txtContains only lowercase characters and space ‘ ‘ characters.
    • Each of the word must consist of lowercase characters only.
    • Words is separated by one or more whitespace characters.

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

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

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

Idea: Count all the words into the llist, through the collection to weight, according to set the key words to check each word frequency

Note: Read TXT file line end to remove/n

Myset=setlist (mylist = list (set)

List.count (number of repetitions of an opt object in the OPTT statistics list

ImportOslist1=[]f= Open ('Words.txt','R') Lines=F.readlines () forEachlineinchLines:list1+=eachline.split () List2=list (set (List1)) Dict= {} forIinchRange (len (list2)): Word=List2[i] Num=List1.count (Word) Dict[word]=NumPrintDict

List type built-in functions list Method operation

List.append (obj) adds an object to the list obj

List.count (obj) returns the number of times an object obj appears in the list

List.extend (seq) a adds the contents of the sequence SEQ to the list

List.index (obj, i=0, J=len (list)) returns the K value of the list[k] = = obj, and the range of K in i<=k<j; otherwise throws a ValueError exception.

List.insert (index, OBJ) inserts the object obj at the index position.

List.pop (Index=-1) a deletes and returns the object at the specified position, which is the last object by default

List.remove (obj) removes an object from the list obj

List.reverse () flip list in place

List.sort (func=none,key=none,reverse=false) sorts the members in the list in the specified manner, if the Func and key parameters are specified, the individual elements are compared in the way specified, if the reverse flag is set to True, the list is ordered in reverse order.

MySet = set (list)

MyList = List (set)

Create 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.