如何在 Python 中進行列表的數學運算?


我們不僅使用列表來儲存一系列值,還用它來進行一些數學計算或運算。

示例 1

import math
data = 21.6
print('The floor of 21.6 is:', math.floor(data))

輸出

The floor of 21.6 is: 21

如何計算列表的加權平均值

示例 2

cost = [0.424, 0.4221, 0.4185, 0.4132, 0.413]
cases = [10, 20, 30, 40, 50]
cost = [23, 10, 5, 32, 41]
weight= [10, 20, 30, 40, 50]
for i in range(len(cost)):
cost[c] = (cost[i] * weight[i] / sum(weight))
cost = sum(cost)
print(cost)

輸出

72.84444444444445

示例 3

import math
degree = 180
radian = math.radians(degree)

print('The given angle is :', radian )
print('sin(x) is :', math.sin(radian ))
print('cos(x) is :', math.cos(radian ))
print('tan(x) is :', math.tan(radian ))

輸出

The given angle is : 3.141592653589793
sin(x) is : 1.2246467991473532e-16
cos(x) is : -1.0
tan(x) is : -1.2246467991473532e-16

以下是幾個 Python 數學函式

  • ceil(x):返回大於或等於 x 的最小整數。
  • copysign(x, y):返回帶有 y 符號的 x。
  • fabs(x):返回 x 的絕對值。
  • factorial(x):返回 x 的階乘。
  • floor(x):返回小於或等於 x 的最大整數。
  • fmod(x, y):返回 x 除以 y 的餘數。
  • frexp(x):返回 x 的尾數和指數,以 (m, e) 對的形式。
  • fsum(iterable):返回可迭代物件中值的精確浮點數和。
  • isfinite(x):如果 x 既不是無窮大也不是 NaN(非數字),則返回 True。
  • isinf(x):如果 x 是正無窮大或負無窮大,則返回 True。
  • isnan(x):如果 x 是 NaN,則返回 True。
  • ldexp(x, i):返回 x * (2**i)。
  • modf(x):返回 x 的小數部分和整數部分。
  • trunc(x):返回 x 的截斷整數。
  • exp(x):返回 e**x。
  • expm1(x):返回 e**x – 1。
  • log(x[, base]):返回 x 以 base 為底的對數(預設為 e)。
  • log1p(x):返回 1+x 的自然對數。
  • log2(x):返回 x 的以 2 為底的對數。

更新於:2019-07-30

8K+ 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始
廣告