Viết mã python để tìm tần số của mỗi từ trong một chuỗi đã cho. Ví dụ: Input : str[] = "Apple Mango Orange Mango Guava Guava Mango" Output : frequency of Apple is : 1 frequency of Mango is : 3 frequency of Orange is : 1 frequency of Guava is : 2 Input : str = "Train Bus Bus Train Taxi Aeroplane Taxi Bus" Output : frequency of Train is : 2 frequency of Bus is : 3 frequency of Taxi is : 2 frequency of Aeroplane is : 1 Được đề xuất: Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp.{IDE} first, before moving on to the solution.Cách tiếp cận 1 bằng danh sách (): & nbsp; 1 Split the string into a list containing the words by using split function (i.e. string.split()) in python with delimiter space. Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book'] 2. Khởi tạo một danh sách trống mới. & NBSP; Initialize a new empty list. 3. Bây giờ hãy nối từ vào danh sách mới từ chuỗi trước nếu từ đó không có trong danh sách mới. & NBSP; Now append the word to the new list from previous string if that word is not present in the new list. 4. Lặp lại trong danh sách mới và sử dụng chức năng đếm (tức là chuỗi.count (Báo chí [Lặp lại])) để tìm tần số của từ ở mỗi lần lặp. Iterate over the new list and use count function (i.e. string.count(newstring[iteration])) to find the frequency of word at each iteration. Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 2 Implementation: Python3
Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0__ Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 str Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']4 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']6 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']8 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0__ Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 21 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 22 str Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 24 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 25 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 26 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 21 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 28 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 22 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 10 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 11 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 12 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0__ Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 21 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 22 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 17 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 18 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 19 Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 30 Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 31 Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 32 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 25 Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 34 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 18 Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 36 Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 37 Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 38 Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 30 str Frequency of Apple : 1 Frequency of Mango : 3 Frequency of Orange : 1 Frequency of Guava : 21
Frequency of Apple : 1 Frequency of Mango : 3 Frequency of Orange : 1 Frequency of Guava : 23 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0____8 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2. Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0 freq( __8Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 26 def 3Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 def 6def 7Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0 def 9Đầu ra Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 1 Cách tiếp cận 2 bằng Set (): & NBSP;
Implementation: Python3
Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0__ Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 str Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']4 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']6 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']8 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0__ Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 21 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 22 str Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 24 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 25 Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 34 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 18 ): 2): 3Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 38 ): 5Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 25 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 26 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 21 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 28 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 22 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 10 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0____8 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2. Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0 freq( str Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']09 Đầu ra Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 3 Cách tiếp cận 2 bằng Set (): & NBSP; Python3Chia chuỗi thành một danh sách chứa các từ bằng cách sử dụng hàm chia (tức là chuỗi.split ()) trong python với không gian phân cách. & Nbsp; Sử dụng phương thức SET () để xóa một bản sao và để đưa ra một tập hợp các từ duy nhất & nbsp; Lặp qua chức năng đếm và sử dụng (tức là chuỗi.count (Báo chí [Lặp lại])) để tìm tần số của từ ở mỗi lần lặp. & Nbsp; ____10 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 str freq( 8____10 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 str 2str 3Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']44 def 7Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 25 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']47 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']16 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']49 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0__ str 6Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 22 str 8Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 26 ): 7__12Cách tiếp cận 3 (sử dụng từ điển):
Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']11 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']64 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']0 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 26 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']14 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']15 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']16 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']17__12 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']20 def 7Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 25 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']23 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']14 Frequency of apple is : 3 Frequency of mango is : 3 Frequency of orange is : 2 Frequency of guava is : 19 def 7Frequency of guava is : 1 Frequency of orange is : 2 Frequency of mango is : 3 Frequency of apple is : 31__ ____10 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 26 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']23 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 22 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']37 Note: string_name.count(substring) is used to find no. of occurrence of substring in a given string. For example: CODE : str='Apple Mango Apple' str.count('Apple') str2='Apple' str.count(str2) OUTPUT : 2 25 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']39 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']40 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']16 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']50 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']2 Note: string_name.split(separator) method is used to split the string by specified separator(delimiter) into the list. If delimiter is not provided then white space is a separator. For example: CODE : str='This is my book' str.split() OUTPUT : ['This', 'is', 'my', 'book']52 Đầu ra Frequency of Apple : 1 Frequency of Mango : 3 Frequency of Orange : 1 Frequency of Guava : 2 |