23 Python常用模块(一)
1. 簡單了解模塊
? ? ?寫的每一個py文件都是一個模塊.
??? 還有一些我們一直在使用的模塊
??? buildins 內置模塊. print, input
??? random 主要是和隨機相關的內容
??????? random()??? 隨機小數
??????? uninform(a,b) 隨機小數
??????? randint(a,b)? 隨機整數
??????? choice() 隨機選擇一個
??????? sample() 隨機選擇多個
??????? shuffle() 打亂
?
2. Collections
??? 1. Counter 計數器
??? 2. defaultdict 默認值字典
??? 3. OrderedDict 有序字典
??? 數據結構(隊列, 棧)
??????? 棧:先進后出
??????????? Stack
class StackFullException(Exception):passclass StackEmptyException(Exception):passclass Stack:def __init__(self,size):self.size = sizeself.lst = []self.top = 0def push(self,el):if self.top >=self.size:raise StackFullException("超范圍了")self.lst.insert(self.top,el)self.top += 1def pop(self):if self.top == 0:raise StackFullException("拿空了")self.top -= 1el = self.lst[self.top]return els = Stack(4)s.push("我") s.push("和") s.push("你") s.push("在")print(s.pop()) print(s.pop()) print(s.pop())隊列: 先進先出
???????? Queue
import queue q = queue.Queue() q.put("李嘉誠1") q.put("李嘉誠2") q.put("李嘉誠3") q.put("李嘉誠4") q.put("李嘉誠5")print(q.get()) print(q.get()) print(q.get()) print(q.get()) print(q.get())3. Time模塊
??? 時間有三種:
??????? 結構化時間 gmtime() localtime()
??????? 時間戳? time.time()? time.mktime()
??????? 格式化時間 time.strftime() time.strptime()
??????? 時間轉化:
??????????? 數字 -> 字符串
??????????? struct_time = time.localtime(數字)
??????????? str = time.strftime("格式", struct_time)
3. Time模塊時間有三種:結構化時間 gmtime() localtime()時間戳 time.time() time.mktime()格式化時間 time.strftime() time.strptime()時間轉化:數字 -> 字符串struct_time = time.localtime(數字)str = time.strftime("格式", struct_time)? 字符串 -> 數字
??????????? struct_time = time.strptime(字符串, "格式")
??????????? num = time.mktime(struct_time)
strt = input("請輸入一個時間") t = time.strptime(strt,"%Y-%m-%d %H:%M:%S") a = time.mktime(t) print(a)4. functools
??? wraps?? 給裝飾器中的inner改名字
??? reduce? 歸納.
??? 偏函數?? 把函數的參數固定.
轉載于:https://www.cnblogs.com/a2534786642/p/10182027.html
總結
以上是生活随笔為你收集整理的23 Python常用模块(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字符串的操作方法(第二天)
- 下一篇: [模板]KMP