Python程式:計算單詞頻率百分比


在這篇文章中,我們將學習如何在Python中計算單詞頻率百分比。

假設我們已經輸入了一個字串列表。現在我們將找到給定輸入字串列表中每個單詞的百分比。

公式

(Occurrence of X word / Total words) * 100

使用的方法

  • 使用sum(),Counter(),join()和split()函式

  • 使用join(),split()和count()函式

  • 使用operator模組中的countOf()函式。

方法1:使用sum(),Counter(),join()和split()函式

join()是Python中的一個字串函式,用於連線由字串分隔符分隔的序列元素。此函式連線序列元素以形成一個字串。

Counter()函式是一個子類,用於計數可雜湊的物件。它在呼叫/呼叫時隱式地建立一個可迭代物件的雜湊表。

演算法(步驟)

以下是執行所需任務的演算法/步驟。

  • 使用import關鍵字從collections模組匯入Counter函式。

  • 建立一個變數來儲存字串的輸入列表並列印列表。

  • 使用join()函式連線輸入列表的所有字串元素。

  • 使用split()函式(將字串分割成列表。我們可以定義分隔符;預設分隔符是任何空格)將連線的字串分割成單詞列表,並使用Counter()函式獲取單詞頻率作為鍵值對。

  • 使用values()函式從Counter獲取所有值(頻率/計數),並使用sum()函式(返回可迭代物件中所有專案的總和)獲取它們的總和。

  • 使用items()函式(返回一個檢視物件,即它包含字典的鍵值對,作為列表中的元組)從上面的計數器單詞獲取每個單詞的百分比。

  • 列印輸入列表中每個單詞的百分比。

示例

以下程式使用sum()、Counter()、join()和split()函式返回給定輸入字串列表中每個單詞的百分比。

# importing a Counter function from the collections module
from collections import Counter

# input list of strings
inputList = ["hello tutorialspoint", "python codes", "tutorialspoint for python", "see python codes tutorialspoint"]
print("Input list:\n", inputList)

# Joining all the string elements of the list using the join() function
join_string = " ".join(i for i in inputList)

# splitting the joined string into a list of words and getting the

# frequency of words as key-value pairs using Counter() function

counter_words = Counter(join_string.split())
# getting all the values(frequencies/counts) from counter and

# finding the total sum of them
total_sum = sum(counter_words.values())

# getting the percentage of each word from the above counter words
res_percentage = {key: value / total_sum for key,
value in counter_words.items()}

# printing the percentage of each word from the input list
print("Percentage of each word from the input list:\n", res_percentage)

輸出

執行後,上述程式將生成以下輸出:

Input list:
['hello tutorialspoint', 'python codes', 'tutorialspoint for python', 'see python codes tutorialspoint']
Percentage of each word from the input list:
{'hello': 0.09090909090909091, 'tutorialspoint': 0.2727272727272727, 'python': 0.2727272727272727, 'codes': 0.18181818181818182, 'for': 0.09090909090909091, 'see': 0.09090909090909091}

方法2:使用join(),split()和count()函式

演算法(步驟)

以下是執行所需任務的演算法/步驟。

  • 建立一個空字典用於儲存結果百分比/單詞頻率。

  • 使用for迴圈遍歷單詞列表。

  • 使用if條件語句,使用keys()函式檢查當前元素是否不在字典的鍵中。

  • 如果上述條件為真,則使用count()函式獲取此鍵(單詞)的計數。

  • 將其除以單詞數以獲取當前單詞頻率,並將其作為鍵儲存在上面建立的新字典中。

  • 列印輸入列表中每個單詞的百分比。

示例

以下程式使用join()、split()和count()函式返回給定輸入字串列表中每個單詞的百分比。

# input list of strings
inputList = ["hello tutorialspoint", "python codes", "tutorialspoint for python", "see python codes tutorialspoint"]

# joining all the elements of the list using join()
join_string = " ".join(i for i in inputList)

# splitting the joined string into a list of words
listOfWords = join_string.split()

# Creating an empty dictionary for storing the resultant percentages
resDict = dict()

# traversing through the list of words
for item in listOfWords:
   
   # checking whether the current element is not in the keys of a dictionary
   if item not in resDict.keys():
      
      # getting the percentage of a current word if the condition is true
      resDict[item] = listOfWords.count(item)/len(listOfWords)

# printing the percentage of each word from the input list
print("Percentage of each word from the input list:\n", resDict)

輸出

執行後,上述程式將生成以下輸出:

Percentage of each word from the input list:
{'hello': 0.09090909090909091, 'tutorialspoint': 0.2727272727272727, 'python': 0.2727272727272727, 'codes': 0.18181818181818182, 'for': 0.09090909090909091, 'see': 0.09090909090909091}

方法3:使用operator模組中的countOf()函式

示例

以下程式使用countOf()函式返回給定輸入字串列表中每個單詞的百分比。

import operator as op
# input list of strings
inputList = ["hello tutorialspoint", "python codes", "tutorialspoint for python", "see python codes tutorialspoint"]

# joining all the elements of list using join()
join_string = " ".join(i for i in inputList)

# splitting the joined string into list of words
listOfWords = join_string.split()
resDict = dict()
for item in listOfWords:
   
   # checking whether the current element is not in the keys of dictionary
   if item not in resDict.keys():
      resDict[item] = op.countOf(listOfWords,   item)/len(listOfWords)
print("Percentage of each word from the input list:\n", resDict)

輸出

執行後,上述程式將生成以下輸出:

Percentage of each word from the input list:
{'hello': 0.09090909090909091, 'tutorialspoint': 0.2727272727272727, 'python': 0.2727272727272727, 'codes': 0.18181818181818182, 'for': 0.09090909090909091, 'see': 0.09090909090909091}

結論

在這篇文章中,我們學習了三種不同的Python方法來計算單詞頻率百分比。我們還學習瞭如何使用operator模組的新函式countOf()來獲取列表元素的頻率。

更新於:2023年1月27日

600 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告