Python sys和math模塊

sys模塊

sys常用的有:

sys.argv

命令行參數List,第一個元素是程序本身路徑
sys.modules.keys()

返回所有已經導入的模塊列表
sys.exc_info()

獲取當前正在處理的異常類,exc_typeexc_valueexc_traceback當前處理的異常詳細信息
sys.exit(n)

退出程序,正常退出時exit(0)
sys.hexversion

獲取Python解釋程序的版本值,16進制格式如:0x020403F0
sys.version

獲取Python解釋程序的版本信息
sys.maxunicode

最大的Unicode
sys.modules

返回系統導入的模塊字段,key是模塊名,value是模塊
sys.path

返回模塊的搜索路徑,初始化時使用PYTHONPATH環境變量的值
sys.platform

返回操作系統平臺名稱
sys.stdout

標準輸出
sys.stdin

標準輸入
sys.stderr

錯誤輸出
sys.exc_clear()

用來清除當前線程所出現的當前的或最近的錯誤信息
sys.exec_prefix

返回平臺獨立的python文件安裝的位置
sys.byteorder

本地字節規則的指示器,big-endian平臺的值是’big’,little-endian平臺的值是’little’
sys.copyright

記錄python版權相關的東西
sys.api_version

解釋器的C的API版本
sys.version_info

Python解釋器的版本信息
sys.displayhook(value)

如果value非空,這個函數會把他輸出到sys.stdout,并且將他保存進__builtin__..指在python的交互式解釋器里,’_'代表上次你輸入得到的結果,hook是鉤子的意思,將上次的結果鉤過來
sys.getdefaultencoding()

返回當前你所用的默認的字符編碼格式
sys.getfilesystemencoding()

返回將Unicode文件名轉換成系統文件名的編碼的名字
sys.setdefaultencoding(name)

用來設置當前默認的字符編碼,如果name和任何一個可用的編碼都不匹配,拋出LookupError,這個函數只會被site模塊的sitecustomize使用,一旦別site模塊使用了,他會從sys模塊移除
sys.builtin_module_names

Python解釋器導入的模塊列表
sys.executable

Python解釋程序路徑
sys.getwindowsversion()

獲取Windows的版本
sys.stdin.readline()

從標準輸入讀一行,sys.stdout.write("a")

屏幕輸出a

math 模塊

math模塊的函數如下:

0,常量

math.pi

π = 3.141592…
math.e

e = 2.718281…

1,數值計算函數

math.ceil(x)

返回≥x的最小整數
math.floor(x)

返回≤x的最大整數
math.copysign(x,y)

返回與y同號的x值
math.fabs(x)

返回x的絕對值
math.factorial(x)

返回x的階乘,即x!,x必須為非負整數
math.fmod(x,y)

返回x對y取模的余數(x決定余數符號),與x%y不同(y決定余數符號)
例:

     math.fmod(100, -3)   -->  1.0
      math.fmod(-100, 3)   --> -1.0
      100 % -3    -->    -2
     -100 %  3    -->     2

math.frexp(x)

返回元組(m,e),根據

x = m*(2**e)
math.fsum(iterable)

返回數組的和,比內置函數sum要精確
math.isfinite(x)

若x是有限數,返回True
math.isinf(x)

若x是無窮大,返回True
math.isnan(x)

若x非數,返回True
math.ldexp(x,i)

返回x*(2**i)的結果
math.modf(x)

返回元組(fractional,integer),分別為x的小數部分和整數部分
math.trunc(x)

返回x的整數部分

2,乘方/對數函數

math.exp(x)

返回e**x
math.expm1(x)

返回e**x - 1
math.log(x[,base])

返回x的對數,base默認的是e
math.log1p(x)

返回x+1的對數,base是e
math.log2(x)

返回x關于2的對數
math.log10(x)

返回x關于10的對數
math.pow(x,y)

返回x**y
math.sqrt(x)

返回x的平方根

3,三角函數

math.sin(x)

返回x的正弦,x用弧度制表示
math.cos(x)

返回x的余弦
math.tan(x)

返回x的正切
math.asin(x)

返回x的反正弦,結果用弧度制表示
math.acos(x)

返回x的反余弦
math.atan(x)

返回x的反正切
math.atan2(y,x)

返回atan(y/x)
math.hypot(x,y)

返回sqrt(x*x + y*y)

4,角度,弧度轉換函數

math.degrees(x)

弧度 –> 角度
math.radians(x)

角度 -> 弧度

5,雙曲線函數

math.acosh(x)

返回x的反雙曲余弦
math.asinh(x)

返回x的反雙曲正弦
math.atanh(x)

返回x的反雙曲正切
math.cosh(x)

返回x的雙曲余弦
math.sinh(x)

返回x的雙曲正弦
math.tanh(x)

返回x的雙曲正切

6,特殊函數

https://docs.python.org/3/library/math.html

math.erf(x)

Return the error function at x.
math.erfc(x)

Return the complementary error function at x
math.gamma(x)

Return the Gamma function at x.
math.lgamma(x)

Return the natural logarithm of the absolute value of the Gamma function at x.

collections模塊

Python擁有一些內置的數據類型,比如str,int, list, tuple, dict等, collections模塊在這些內置數據類型的基礎上,提供了幾個額外的數據類型:

  1. namedtuple 生成可以使用名字來訪問元素內容的tuple子類
  2. deque 雙端隊列,可以快速的從另外一側追加和推出對象
  3. Counter 計數器,主要用來計數
  4. OrderedDict 有序字典
  5. defaultdict 帶有默認值的字典

heapq模塊

https://docs.python.org/2/library/heapq.html

這個模塊(build-in)實現了一個堆的數據結構,完美的解決了Top-K問題,以后解決Top-K問題的時候,直接把這個模塊拿來用就可以了
注意,默認的heap是一個小頂堆!

heapq模塊提供了如下幾個函數:

heapq.heappush(heap, item)把item添加到heap中(heap是一個列表)
heapq.heappop(heap)

把堆頂元素彈出,返回的就是堆頂
heapq.heappushpop(heap, item)

先把item加入到堆中,然后再pop,比heappush()再heappop()要快得多
heapq.heapreplace(heap, item)先pop,然后再把item加入到堆中,比heappop()再heappush()要快得多
heapq.heapify(x)將列表x進行堆調整,默認的是小頂堆
heapq.merge(*iterables)將多個列表合并,并進行堆調整,返回的是合并后的列表的迭代器
heapq.nlargest(n, iterable, key=None)

返回最大的n個元素(Top-K問題)
heapq.nsmallest(n, iterable, key=None)返回最小的n個元素(Top-K問題

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容