生活随笔
收集整理的這篇文章主要介紹了
数值与日期
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、數(shù)值
1. 格式化
一般形式 ‘?dāng)?shù)值:{:}’.format(a[0]) 顯示正負(fù)號(hào) ‘分?jǐn)?shù):{:-}’.format(a[0]) #顯示正負(fù)號(hào) 顯示幾位小數(shù) ‘?dāng)?shù)值:{:.2f}’.format(a[0]) 顯示百分比 ‘分?jǐn)?shù):{:.2%}’.format(1/5)
2.小數(shù)位處理
math.trunc() 截去小數(shù)部分
import math
a
= [ - 2.9 , - 2.4 , 2.4 , 2.9 ]
result
= map ( math
. trunc
, a
)
print ( list ( result
) )
[ - 2 , - 2 , 2 , 2 ]
math.ceil() 向上取整
import math
a
= [ - 2.9 , - 2.4 , 2.4 , 2.9 ]
result
= map ( math
. ceil
, a
)
print ( list ( result
) )
[ - 2 , - 2 , 3 , 3 ]
math.floor() 向下取整
import math
a
= [ - 2.9 , - 2.4 , 2.4 , 2.9 ]
result
= map ( math
. floor
, a
)
print ( list ( result
) )
[ - 3 , - 3 , 2 , 2 ]
round() 四舍五入
import math
a
= [ - 2.9 , - 2.4 , 2.4 , 2.9 ]
result
= map ( round , a
)
print ( list ( result
) )
[ - 3 , - 2 , 2 , 3 ]
二、隨機(jī)數(shù)
random.choice(lst)從序列 中隨機(jī)選擇一個(gè)元素 random.sample(lst,n)從序列 中隨機(jī)選擇n個(gè)元素 random.shuffle(lst) 隨機(jī)打亂一個(gè)可變序列 ,改變原序列,無返回。不支持字符串、元組等不可變序列 random.randint(a,b)#生成指定范圍內(nèi)的隨機(jī)整數(shù)N。 a<=N<=b random.random()#生成隨機(jī)浮點(diǎn)數(shù) [0.0, 1.0). random.getrandbits(K)# 生成2^k位數(shù)的隨機(jī)數(shù)
import random
lst
= 'abcde'
print ( lst
)
print ( random
. choice
( lst
) )
print ( random
. choice
( lst
) )
print ( random
. choice
( lst
) )
print ( random
. sample
( lst
, 3 ) )
print ( random
. shuffle
( lst
) )
print ( lst
)
abcde
e
c
e
['d', 'c', 'b']
abcde
import random
a
= 1
b
= 10
print ( random
. randint
( a
, b
) )
print ( random
. random
( ) )
print ( random
. getrandbits
( 5 ) )
10
0.45699859741584814
31
shuffle 和 permutation 的區(qū)別
三、日期時(shí)間
1. datetime模塊
date 處理日期(有年月日屬性) time 處理時(shí)間 (有時(shí)分秒屬性) datetime 處理日期與時(shí)間 (有年月日時(shí)分秒屬性)
import datetime
birth
= datetime
. date
( 2019 , 8 , 21 )
t
= datetime
. time
( 10 , 1 , 20 )
t2
= datetime
. datetime
( 2019 , 8 , 21 , 7 , 30 , 0 )
now
= datetime
. datetime
. now
( )
print ( '構(gòu)造日期:' , birth
)
print ( '年:' , birth
. year
)
print ( '構(gòu)造時(shí)間:' , t
)
print ( '時(shí):' , t
. hour
)
print ( '構(gòu)造日期與時(shí)間:' , t2
)
print ( '自帶的此時(shí)時(shí)間:' , now
)
構(gòu)造日期: 2019-08-21
年: 2019
構(gòu)造時(shí)間: 10:01:20
時(shí): 10
構(gòu)造日期與時(shí)間: 2019-08-21 07:30:00
自帶的此時(shí)時(shí)間: 2019-08-21 20:17:36.400167
2. 格式轉(zhuǎn)換
字符串轉(zhuǎn)到日期 t = datetime.datetime.strptime(s,’%Y-%m-%d’) 日期轉(zhuǎn)到字符串 s=now.strftime(’%Y-%m-%d’)
s
= '2019-8-21'
t
= datetime
. datetime
. strptime
( s
, '%Y-%m-%d' )
print ( '字符串 ' , s
)
print ( '轉(zhuǎn)后的時(shí)間 ' , t
)
now
= datetime
. datetime
. now
( )
print ( '時(shí)間 ' , now
)
s_now
= now
. strftime
( '%Y~%m~%d' )
print ( '轉(zhuǎn)后的字符串 ' , s_now
)
字符串 2019-8-21
轉(zhuǎn)后的時(shí)間 2019-08-21 00:00:00
時(shí)間 2019-08-21 20:07:52.090167
轉(zhuǎn)后的字符串 2019~08~21
占位符 %Y 四位年份 %y 二位年份 %m 二位月份 %d 二位日期 %H 二位小時(shí) %M 二位分鐘 %S 二位秒數(shù) %f 微秒
3. 時(shí)間差 timedelta
days seconds
t1
= datetime
. datetime
( 1919 , 1 , 1 , 7 , 30 , 0 )
t2
= datetime
. datetime
( 2019 , 8 , 21 , 7 , 30 , 30 )
diff
= t2
- t1
print ( diff
)
print ( '相減后日期' , diff
. days
)
print ( '相減后秒' , diff
. seconds
)
print ( '相減days*24+seconds' , diff
. total_seconds
( ) )
36757 days, 0:00:30
相減后日期 36757
相減后秒 30
相減days*24+seconds 3175804830.0
t1
= datetime
. date
( 2019 , 8 , 21 )
print ( '當(dāng)前時(shí)間 ' , t1
)
t2
= t1
+ datetime
. timedelta
( days
= - 15 )
print ( '前移15天' , t2
)
t3
= t1
+ datetime
. timedelta
( days
= 26 )
print ( '往后26天' , t3
)
當(dāng)前時(shí)間 2019-08-21
前移15天 2019-08-06
往后26天 2019-09-16
總結(jié)
以上是生活随笔 為你收集整理的数值与日期 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。