模組( collections , time , random , os , sys)

來源:互聯網
上載者:User

標籤:環境   version   rand   data   time()   轉化   模組   get   元組   

認識模組: 一條代碼 < 語句塊 < 代碼塊(函數, 類) < 模組.

collections (克萊克森斯)

1. Counter  #用來查看字元出現的次數.
s = "upup qwe"print(Counter(s))

隊列: FI FO. 先進先出  棧:  FI LO. 先進後出

2.deque(雙向隊列)from collections import deques = deque()s.append("娃哈哈")s.append("營養快線")s.appendleft("爽歪歪") # 左側添加print(s)print(s.pop())  print(s.popleft()) #左側刪除
3.namedtuple (命名元組)# from collections import namedtuple# nt = namedtuple("point", ["x", "y"])# p = nt(1, 2)# print(p)# print(p.x)# print(p.y)

time 時間模組

python中時間分成三種表現形式:

 1.  時間戳記(timestamp)

import timeprint(time.time())

 

2.  格式化時間(strftime)

import time
s = time.strftime("%Y-%m-%d %H:%M:%S")print(s)

 

 3.  結構化時間(struct_time)

print(time.localtime())結果: time.struct_time(tm_year=2017, tm_mon=05, tm_mday=8, tm_hour=10, tm_min=24, tm_sec=42, tm_wday=0, tm_yday=126, tm_isdst=0)
所有的轉化都要通過結構化時間來轉化.

 

random 隨機數模組

import randomprint(random.random())  # 0-1小數 print(random.uniform(3,10)) 3-10小數 print(random.randint(1,10)) 1-10整數print(random.randrange(1,10,2)) 1-10奇數 print(random.choice([1, ‘周杰倫‘, ["蓋倫", "胡辣湯"]]))1或者23或者[4,5]) print(random.sample([1, ‘23‘, [4, 5]], 2)) 列表元素任意2個組合 random.shuffle(lst)  # 隨機打亂順序

os 和作業系統相關的模組

import osos.makedirs(‘a/b/c/d‘) 建立多層os.removedirs(‘dirname1‘)刪除os.mkdir(‘dirname‘)建立單層os.rmdir(‘dirname‘)刪除單層os.rename("oldname","newname")重新命名os.getcwd()擷取當前?作目錄

sys 和python解譯器相關的模組

 

sys.argv           命令列參數List,第一個元素是程式本身路徑sys.exit(n)        退出程式,正常退出時exit(0),錯誤退出sys.exit(1)sys.version        擷取Python解釋程式的版本資訊 sys.path           返回模組的搜尋路徑,初始化時使?PYTHONPATH環境變數的值 sys.platform       返回作業系統平台名稱

 

模組( collections , time , random , os , sys)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.