搜尋
首頁後端開發Python教學Python程式取得單字頻率的百分比

Python程式取得單字頻率的百分比

Sep 08, 2023 pm 06:29 PM
python百分比單字頻率

Python程式取得單字頻率的百分比

在本文中,我們將學習如何在Python中以百分比形式取得詞頻。

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

公式

(Occurrence of X word / Total words) * 100

使用的方法

  • 使用sum()、Counter()、join()和split()函數

  • #使用 join()、split() 和 count() 函數

  • #使用operator模組的countOf()函數。

方法一:使用 sum()、Counter()、join() 和 split() 函數

join() 是Python中的字串函數,用於將由字串分隔符號分隔的序列元素連接起來,形成一個字串。

Counter() 函數是計算可雜湊物件數的子類別。它在調用/調用時隱式創建可迭代對象的哈希表。

演算法(步驟)

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

  • 使用 import 關鍵字從集合模組匯入 Counter 函數。

  • 建立一個變數來儲存輸入清單字串並列印該清單。

  • 使用join()函數連接輸入清單的所有字串元素。

  • 使用split() 函數(將字串分割為列表。可以定義分隔符號;預設分隔符號為任意空白字元)將連接的字串分割為單字列表,並使用Counter() 函數取得單字頻率作為鍵值對

  • #使用values()函數從Counter中取得所有值(頻率/計數),並使用sum()函數取得它們的總和(傳回所有值的總和)可迭代中的專案).

  • 使用items()函數取得上述計數器單字中每個單字的百分比(傳回一個視圖對象,即它包含字典的鍵值對,作為元組在列表中)。

  • 列印輸入清單中每個單字的百分比。

Example

的中文翻譯為:

範例

以下程式使用 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()函數取得該鍵(單字)的計數。

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

  • 列印輸入清單中每個單字的百分比。

Example

的中文翻譯為:

範例

以下程式使用 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}

方法三:使用operator模組的countOf()函數

Example

的中文翻譯為:

範例

以下程式使用 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 方法來計算百分比詞頻。我們也學習如何使用運算元模組的新函數 countOf() 來取得清單元素的頻率。

以上是Python程式取得單字頻率的百分比的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:tutorialspoint。如有侵權,請聯絡admin@php.cn刪除
Python:探索其主要應用程序Python:探索其主要應用程序Apr 10, 2025 am 09:41 AM

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

您可以在2小時內學到多少python?您可以在2小時內學到多少python?Apr 09, 2025 pm 04:33 PM

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎?如何在10小時內通過項目和問題驅動的方式教計算機小白編程基礎?Apr 02, 2025 am 07:18 AM

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到?如何在使用 Fiddler Everywhere 進行中間人讀取時避免被瀏覽器檢測到?Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python 3.6加載Pickle文件報錯"__builtin__"模塊未找到怎麼辦?Python 3.6加載Pickle文件報錯"__builtin__"模塊未找到怎麼辦?Apr 02, 2025 am 07:12 AM

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何提高jieba分詞在景區評論分析中的準確性?如何提高jieba分詞在景區評論分析中的準確性?Apr 02, 2025 am 07:09 AM

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...

如何使用正則表達式匹配到第一個閉合標籤就停止?如何使用正則表達式匹配到第一個閉合標籤就停止?Apr 02, 2025 am 07:06 AM

如何使用正則表達式匹配到第一個閉合標籤就停止?在處理HTML或其他標記語言時,常常需要使用正則表達式來�...

如何繞過Investing.com的反爬蟲機制獲取新聞數據?如何繞過Investing.com的反爬蟲機制獲取新聞數據?Apr 02, 2025 am 07:03 AM

攻克Investing.com的反爬蟲策略許多人嘗試爬取Investing.com(https://cn.investing.com/news/latest-news)的新聞數據時,常常�...

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中