溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python常用數(shù)字處理基本操作匯總

發(fā)布時(shí)間:2020-09-19 00:35:32 來源:腳本之家 閱讀:125 作者:Jimmyhe 欄目:開發(fā)技術(shù)

一些基本的操作,在工作者遇到相關(guān)問題要有相關(guān)印象。

一、 你想對浮點(diǎn)數(shù)執(zhí)行指定精度的舍入運(yù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ù)。 也就是說,對1.5或者2.5的舍入運(yùn)算都會得到2。

傳給 round() 函數(shù)的 ndigits 參數(shù)可以是負(fù)數(shù),這種情況下, 舍入運(yùn)算會作用在十位、百位、千位等上面。比如:

>>> a = 1627731
>>> round(a, -1)
1627730
>>> round(a, -2)
1627700
>>> round(a, -3)
1628000
>>>

不要將舍入和格式化輸出搞混淆了。 如果你的目的只是簡單的輸出一定寬度的數(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ù)字符串,簡單的使用帶有進(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.numerator
35
>>> c.denominator
64

>>> # 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())
>>> y
Fraction(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ù),請使用 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
>>>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI