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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

在CentOS系統上高效訓練PyTorch模型,需要分步驟進行,本文將提供詳細指南。一、環境準備:Python及依賴項安裝:CentOS系統通常預裝Python,但版本可能較舊。建議使用yum或dnf安裝Python3併升級pip:sudoyumupdatepython3(或sudodnfupdatepython3),pip3install--upgradepip。 CUDA與cuDNN(GPU加速):如果使用NVIDIAGPU,需安裝CUDATool

在CentOS系統上啟用PyTorchGPU加速,需要安裝CUDA、cuDNN以及PyTorch的GPU版本。以下步驟將引導您完成這一過程:CUDA和cuDNN安裝確定CUDA版本兼容性:使用nvidia-smi命令查看您的NVIDIA顯卡支持的CUDA版本。例如,您的MX450顯卡可能支持CUDA11.1或更高版本。下載並安裝CUDAToolkit:訪問NVIDIACUDAToolkit官網,根據您顯卡支持的最高CUDA版本下載並安裝相應的版本。安裝cuDNN庫:前

Docker利用Linux內核特性,提供高效、隔離的應用運行環境。其工作原理如下:1. 鏡像作為只讀模板,包含運行應用所需的一切;2. 聯合文件系統(UnionFS)層疊多個文件系統,只存儲差異部分,節省空間並加快速度;3. 守護進程管理鏡像和容器,客戶端用於交互;4. Namespaces和cgroups實現容器隔離和資源限制;5. 多種網絡模式支持容器互聯。理解這些核心概念,才能更好地利用Docker。

Python和JavaScript在社區、庫和資源方面的對比各有優劣。 1)Python社區友好,適合初學者,但前端開發資源不如JavaScript豐富。 2)Python在數據科學和機器學習庫方面強大,JavaScript則在前端開發庫和框架上更勝一籌。 3)兩者的學習資源都豐富,但Python適合從官方文檔開始,JavaScript則以MDNWebDocs為佳。選擇應基於項目需求和個人興趣。

在CentOS下選擇PyTorch版本時,需要考慮以下幾個關鍵因素:1.CUDA版本兼容性GPU支持:如果你有NVIDIAGPU並且希望利用GPU加速,需要選擇支持相應CUDA版本的PyTorch。可以通過運行nvidia-smi命令查看你的顯卡支持的CUDA版本。 CPU版本:如果沒有GPU或不想使用GPU,可以選擇CPU版本的PyTorch。 2.Python版本PyTorch

MinIO對象存儲:CentOS系統下的高性能部署MinIO是一款基於Go語言開發的高性能、分佈式對象存儲系統,與AmazonS3兼容。它支持多種客戶端語言,包括Java、Python、JavaScript和Go。本文將簡要介紹MinIO在CentOS系統上的安裝和兼容性。 CentOS版本兼容性MinIO已在多個CentOS版本上得到驗證,包括但不限於:CentOS7.9:提供完整的安裝指南,涵蓋集群配置、環境準備、配置文件設置、磁盤分區以及MinI

CentOS 安裝 Nginx 需要遵循以下步驟:安裝依賴包,如開發工具、pcre-devel 和 openssl-devel。下載 Nginx 源碼包,解壓後編譯安裝,並指定安裝路徑為 /usr/local/nginx。創建 Nginx 用戶和用戶組,並設置權限。修改配置文件 nginx.conf,配置監聽端口和域名/IP 地址。啟動 Nginx 服務。需要注意常見的錯誤,如依賴問題、端口衝突和配置文件錯誤。性能優化需要根據具體情況調整,如開啟緩存和調整 worker 進程數量。
