Python常用數(shù)字處理基本操作匯總
一些基本的操作,在工作者遇到相關(guān)問題要有相關(guān)印象。
一、 你想對(duì)浮點(diǎn)數(shù)執(zhí)行指定精度的舍入運(yùn)算
對(duì)于簡(jiǎn)單的舍入運(yùn)算,使用內(nèi)置的 round(value, ndigits) 函數(shù)即可。比如:
>>> round(1.23, 1)1.2>>> round(1.27, 1)1.3>>> round(-1.27, 1)-1.3>>> round(1.25361,3)1.254>>>
當(dāng)一個(gè)值剛好在兩個(gè)邊界的中間的時(shí)候, round 函數(shù)返回離它最近的偶數(shù)。 也就是說,對(duì)1.5或者2.5的舍入運(yùn)算都會(huì)得到2。
傳給 round() 函數(shù)的 ndigits 參數(shù)可以是負(fù)數(shù),這種情況下, 舍入運(yùn)算會(huì)作用在十位、百位、千位等上面。比如:
>>> a = 1627731>>> round(a, -1)1627730>>> round(a, -2)1627700>>> round(a, -3)1628000>>>
不要將舍入和格式化輸出搞混淆了。 如果你的目的只是簡(jiǎn)單的輸出一定寬度的數(shù),你不需要使用 round() 函數(shù)。 而僅僅只需要在格式化的時(shí)候指定精度即可。比如:
>>> x = 1.23456>>> format(x, ’0.2f’)’1.23’>>> format(x, ’0.3f’)’1.235’>>> ’value is {:0.3f}’.format(x)’value is 1.235’>>>
二、進(jìn)制轉(zhuǎn)化
為了將整數(shù)轉(zhuǎn)換為二進(jìn)制、八進(jìn)制或十六進(jìn)制的文本串, 可以分別使用 bin() , oct() 或 hex()函數(shù):
>>> x = 1234>>> bin(x)’0b10011010010’>>> oct(x)’0o2322’>>> hex(x)’0x4d2’>>>
為了以不同的進(jìn)制轉(zhuǎn)換整數(shù)字符串,簡(jiǎn)單的使用帶有進(jìn)制的 int() 函數(shù)即可:
>>> int(’4d2’, 16)1234>>> int(’10011010010’, 2)1234>>>
三、分?jǐn)?shù)相關(guān)運(yùn)算
>>> from fractions import Fraction>>> a = Fraction(5, 4)>>> b = Fraction(7, 16)>>> print(a + b)27/16>>> print(a * b)35/64
>>> # Getting numerator/denominator>>> c = a * b>>> c.numerator35>>> c.denominator64
>>> # Converting to a float>>> float(c)0.546875
>>> # Limiting the denominator of a value>>> print(c.limit_denominator(8))4/7
>>> # Converting a float to a fraction>>> x = 3.75>>> y = Fraction(*x.as_integer_ratio())>>> yFraction(15, 4)>>>
四、random模塊
random 模塊有大量的函數(shù)用來產(chǎn)生隨機(jī)數(shù)和隨機(jī)選擇元素。 比如,要想從一個(gè)序列中隨機(jī)的抽取一個(gè)元素,可以使用 random.choice() :
>>> import random>>> values = [1, 2, 3, 4, 5, 6]>>> random.choice(values)2>>> random.choice(values)3>>> random.choice(values)1>>> random.choice(values)4>>> random.choice(values)6>>>
為了提取出N個(gè)不同元素的樣本用來做進(jìn)一步的操作,可以使用 random.sample() :
>>> random.sample(values, 2)[6, 2]>>> random.sample(values, 2)[4, 3]>>> random.sample(values, 3)[4, 3, 1]>>> random.sample(values, 3)[5, 4, 1]>>>
如果你僅僅只是想打亂序列中元素的順序,可以使用 random.shuffle() :
>>> random.shuffle(values)>>> values[2, 4, 6, 5, 3, 1]>>> random.shuffle(values)>>> values[3, 5, 2, 1, 6, 4]>>>
生成隨機(jī)整數(shù),請(qǐng)使用 random.randint() :
>>> random.randint(0,10)2>>> random.randint(0,10)5>>> random.randint(0,10)0>>> random.randint(0,10)7>>> random.randint(0,10)10>>> random.randint(0,10)3>>>
為了生成0到1范圍內(nèi)均勻分布的浮點(diǎn)數(shù),使用 random.random() :
>>> random.random()0.9406677561675867>>> random.random()0.133129581343897>>> random.random()0.4144991136919316>>>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS3中Transition屬性詳解以及示例分享2. ASP基礎(chǔ)入門第八篇(ASP內(nèi)建對(duì)象Application和Session)3. jsp文件下載功能實(shí)現(xiàn)代碼4. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法5. asp.net core項(xiàng)目授權(quán)流程詳解6. html中的form不提交(排除)某些input 原創(chuàng)7. ASP常用日期格式化函數(shù) FormatDate()8. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效9. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享10. XMLHTTP資料
