Python常用數字處理基本操作彙總

2022-10-03 09:09:09 字數 3215 閱讀 9714

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

一、 你想對浮點數執行指定精度的捨入運算

對於簡單的捨入運算,使用內建的 round(value, ndigits) 函式即可。比如:

>>> round(1.23, 1)

1.2>>> round(1.27, 1)

1.3>>> round(-1.27, 1)

-1.3

>>> round(1.25361,3)

1.254

>>>

當乙個值剛好在兩個邊界的中間的時候, round 函式返回離它最近的偶數。 也就是說,對1.5或者2.5的捨入運算都會得到2。

傳給 round() 函式的 ndigits 引數可以是負數,這種情況下, 捨入運算會作用在十位、百位、千位等上面。比如:

>>> a = 1627731

>>> round(a, -1)

1627730

>>> round(a, -2)

1627700

>>> round(a, -3)

1628000

>>>

不要將捨入和格式化輸出搞混淆了。 如果你的目的只是簡單的輸出一定寬度的數,你不需要使用 round() 函式。 而僅僅只需要在格式化的時候指定精度即可。比如:

>>> x = 1.23456

>>> format(x, '0.2f')

'1.23'

>>> format(x, '0.3f')

'1.235'

>>> 'value is '.format(x)

'value is 1.235'

>>>

二、進製轉化

為了將整數轉換為二進位制、八進位制或十六進製制的文字串, 可以分別使用 bin() , oct() 或 hex()函式:

>>> x = 1234

>>> bin(x)

'0b10011010010'

>>> oct(x)

'0o2322'

>>> hex(x)

'0x4d2'

>>>

為了程式設計客棧以不同的進製轉換整數字串,簡單的使用帶有進製的 int() 函式即可:

>>> int('4d2', 16)

1234

>>> int('10011010010', 2)

1234

>>>

三、分數相關運算

>>> 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 模組有大量的函式用來產生隨機數和隨機選擇元素。 比如,要想從乙個序列中隨機的抽取乙個元素,可以使用 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個不同元素的樣本用來做進一步的操作,可以使用 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]

>>>

生成隨機整數,請使用 random.randint() :

>>> rwww.cppcns.comandom.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範圍內均勻分布的浮點數,使用 random.random() :

>>> random.random()

0.9406677561675867

>www.cppcns.com>> random.random()

0.133129581343897

>>> random.random()

0.4144991136919316

>>>

本文標題: python常用數字處理基本操作彙總

本文位址: /jiaoben/python/344001.html

XDOJ 數字處理

標題 數字處理 類別函式與遞迴 時間限制 2s記憶體限制 1000kb 問題描述 編寫乙個程式,從鍵盤輸入乙個非零整數n 0 n 1000000000 對整數n進行如下處理 將整數的各位數字取出來相加,如果結果是一位數則輸出該數,否則重複上述過程,直到得到的結果為一位數,並輸出該結果。例如 n 45...

JS數字處理

在前端開發的過程中經常會遇到做一些數字處理 比如js精度問題等等 以下是我的一些總結。有一些資料金額比較大,需要每隔三個數字打乙個逗號。如下 處理數字 打逗號 param str export const handalnumber str g,1,replace split reverse join...

PHP數字處理

援引自博文 ceil 進一法取整 float ceil float value 返回不小於 value 的下乙個整數,value 如果有小數部分則進一位。ceil 返回的型別仍然是 float,因為 float 值的範圍通常比 integer 要大。eg.php echo ceil 2.3 3 ec...