歡迎您光臨本站 註冊首頁

Python中有幾個關鍵字

←手機掃碼閱讀     hongdian2012 @ 2020-06-10 , reply:0

Python中關鍵詞有多少個?Python中關鍵詞目前有31個,可以利用Python的內置的keyword模塊進行輸出查看。

keyword模塊

  Help on module keyword:  NAME   keyword - Keywords (from "graminit.c")  FILE   /usr/lib64/python2.6/keyword.py  DESCRIPTION   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.py  FUNCTIONS   iskeyword = __contains__(...)    x.__contains__(y) y in x.  DATA   __all__ = ['iskeyword', 'kwlist']   kwlist = ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', ...

 

得到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']

 

判斷字符串是否是python的關鍵字

  >>> keyword.iskeyword('and')  True  >>>   >>> keyword.iskeyword('has')  False

 

關於關鍵字知識點擴展:

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文檔的篇數)


[hongdian2012 ] Python中有幾個關鍵字已經有244次圍觀

http://coctec.com/docs/python/shhow-post-237889.html