Python+常用模組(2).md

來源:互聯網
上載者:User

標籤:微軟   amp   3.2   get   lan   nta   常用模組   flex   col   

Python 常用模組1. random模組1.1 匯入模組

import random

1.2 random.random()

產生一個從0到1的隨機浮點數

1.3 random.uniform(10,20)

產生指點範圍內,也就是10到20(包括10和20,且位置可以互換)之間的隨機數

1.4 random.randint(a,b)

產生a到b之間的整數

1.5 random.randrange([start],stop[,step])

從指定集合中抽取隨機數,

1.6 random.choice(sequence)

從序列中擷取一個隨機元素,序列可以使list、字串等.
與np.random.choice(a,size=None..)相比,只能抽一個

1.7 random.shuffle(x[,random])

x為列表,用於將列表打亂

1.8 random.sample(seqnence,k)

從制定序列中擷取指定長度的片段。

1.9 random.randint()

隨機整數

2. time模組 2.1 匯入模組

import time

2.2 time.time()

返回目前時間戳

2.3 time.localtime()

格式化時間戳記為本地時間

2.4 time.asctime(time.localtime)

接受時間元組並返回一個可讀的字串

2.5 time.ctime()

時間戳記轉化為asctime的格式,可讀

2.6 time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())

格式化時間元組

2.7 time.sleep()

進程掛起時間。

3. datetime模組 3.1 匯入模組

import datetime

3.2 datetime.datetime.now()

 擷取當前datetime

3.3 datetime.date.today()

擷取當天date,返回2018,4,8

3.4 擷取明天/前n天

datetime.date.today()+datatime.timedelta(days=1)

3.5 兩個datetime的時間差

相減返回時間戳記

3.6 關係轉換 3.6.1 datetime->string

datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

3.6.2 string->datetime

datetime.datetime.strptime("2014-12-31 18:20:10", "%Y-%m-%d %H:%M:%S")

3.6.3 datetime->timestamp

now = datetime.datetime.now()
timestamp = time.mktime(now.timetuple())
timestamp

3.6.3 timestamp->datetime

datetime.datetime.fromtimestamp(1421077403.0)

3.6.4 datetime->date

datetime.datetime.now().date()

3.6.5 date->datetime

today = datetime.date.today()
datetime.datetime.combine(today, datetime.time())

Python+常用模組(2).md

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.