如何查看python關鍵字
現在我們就來講講關鍵字吧,準備好開始記筆記了吧,俗話說得好,好記性不如爛筆頭,記好了喔,經常聽大家提起關于Python中關鍵詞有多少個?實際上Python中關鍵詞目前有31個,想要具體查看以及觀察個數的方式非常簡單,下面就為大家詳細介紹。
1、keyword模塊進行輸出查看
Help on module keyword:NAME keyword - Keywords (from 'graminit.c')FILE /usr/lib64/python2.6/keyword.pyDESCRIPTION This file is automatically generated; please don’t muck it up! To update the symbols in this file, ’cd’ to the top directory of the python source tree after building the interpreter and run: python Lib/keyword.pyFUNCTIONS iskeyword = __contains__(...) x.__contains__(y) y in x.DATA __all__ = [’iskeyword’, ’kwlist’] kwlist = [’and’, ’as’, ’assert’, ’break’, ’class’, ’continue’, ’def’, ...
2、得到python關鍵字列表
>>> keyword.kwlist[’and’, ’as’, ’assert’, ’break’, ’class’, ’continue’, ’def’, ’del’, ’elif’, ’else’, ’except’, ’exec’, ’finally’, ’for’, ’from’, ’global’, ’if’, ’import’, ’in’, ’is’, ’lambda’, ’not’, ’or’, ’pass’, ’print’, ’raise’, ’return’, ’try’, ’while’, ’with’, ’yield’]
3、判斷字符串是否含關鍵字
>>> keyword.iskeyword(’and’)True>>> >>> keyword.iskeyword(’has’)False
Python關鍵字知識點擴展:
TF-IDF
TF-IDF(Term Frequencey-Inverse Document Frequency)指詞頻-逆文檔頻率,它屬于數值統計的范疇。使用TF-IDF,我們能夠學習一個詞對于數據集中的一個文檔的重要性。
TF-IDF的概念
TF-IDF有兩部分,詞頻和逆文檔頻率。首先介紹詞頻,這個詞很直觀,詞頻表示每個詞在文檔或數據集中出現的頻率。等式如下:
TF(t)=詞t在一篇文檔中出現的次數/這篇文檔的總詞數
第二部分——逆文檔頻率實際上告訴了我們一個單詞對文檔的重要性。這是因為當計算TF的時候,我們對每個詞賦予了同等的重要性,它出現得越多,它的TF就越高,如果它出現了100次,也許相比其他出現更少的詞,它并不攜帶那么多信息,因此我們需要賦予它們權重,決定每個詞的重要性。使用下面的等式得到IDF:
IDF(t)=(log10文檔的篇數/包含詞t文檔的篇數)
那么,計算TF-IDF的方法如下:
TF * IDF=(詞t在一篇文檔中出現的次數/這篇文檔的總詞數)* log10(文檔的篇數/包含詞t文檔的篇數)
到此這篇關于如何查看python關鍵字的文章就介紹到這了,更多相關python關鍵字查看實例內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
