こんにちはみなさん!この記事では、 Pythonリスト内の平均を見つけるさまざまな
方法について見ていきます。
一般的に、平均値とはデータ項目や要素全体を表す値です。
Pythonでリストの平均を見つけるためのテクニック
以下のいずれかのテクニックを使用してPythonのリストの平均/平均を計算できます。
- Python mean() 関数
- 組み込みの sum() メソッド
- Python lambda と reduce() メソッド
- Python operator.add() メソッド
1. Python mean() 関数
Python 3には、平均値または平均を計算するための組み込み関数を含むstatisticsモジュール
があります。 statistics.mean() 関数
は、入力値またはデータセットの平均/平均を計算するために使用されます。
mean()関数は、数値を含むリスト、タプル、またはデータセットをパラメータとして受け取り、データアイテムの平均値を返します。
構文:
mean(data-set/input-values)
例:
from statistics import mean
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
list_avg = mean(inp_lst)
print("Average value of the list:\n")
print(list_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(list_avg,3))
上記のコードスニペットでは、statistics.round()
メソッドを使用して、出力平均を特定の小数値に丸めました。。
構文:
statistics.round(value, precision value)
出力:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
2. Pythonのsum()関数を使用する
Python statistics.sum()
関数は、Pythonリスト内のデータ値の平均を見つけるためにも使用できます。
statistics.len()
関数は、リストの長さ、つまりリスト内のデータアイテムの数を計算するために使用されます。
構文:
len(input-list)
さらに、statistics.sum()
関数を使用して、リスト内のすべてのデータアイテムの合計を計算します。
構文:
sum(input-list)
注意: 平均 = (合計)/(カウント).
例:
from statistics import mean
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
sum_lst = sum(inp_lst)
lst_avg = sum_lst/len(inp_lst)
print("Average value of the list:\n")
print(lst_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))
出力:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
3. Pythonのreduce()とlambdaメソッドを使用する
Pythonのreduce()関数をlambda()関数と組み合わせて使用することができます。
Pythonのreduce()関数: reduce()関数
は、基本的に、関数が適用される特定の(入力)関数を関数に渡された要素のセットに適用するために使用されます。
構文:
reduce(function,input-list/sequence)
- 最初に、reduce()関数は、渡された関数を最初の2つの連続する要素に適用し、結果を返します。
- さらに、前のステップで得られた結果と2番目の要素の後続要素に同じ関数を適用します。
- このプロセスは、リストの末尾に到達するまで続きます。
- 最後に、結果が端末/画面に出力されます。
Pythonのlambda()関数:lambda()関数
は、名前やシグネチャのない匿名関数を構築して形成するために使用されます。
構文:
lambda arguments:function
例:
from functools import reduce
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
lst_len= len(inp_lst)
lst_avg = reduce(lambda x, y: x + y, inp_lst) /lst_len
print("Average value of the list:\n")
print(lst_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))
出力:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
4. Python operator.add()関数を使用してリストの平均を見つける
Pythonのoperatorモジュールには、効率的に基本的な計算と操作を実行するためのさまざまな関数が含まれています。
operator.add()
関数は、Pythonのreduce()関数を使用して、リスト内のすべてのデータ値の合計を計算するために使用できます。
構文:
operator.add(value1, value2)
注意: 平均 = (合計)/(要素の長さまたは個数)
例:
from functools import reduce
import operator
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
lst_len = len(inp_lst)
lst_avg = reduce(operator.add, inp_lst) /lst_len
print("Average value of the list:\n")
print(lst_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))
出力:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
5. Pythonでリストの平均を計算するためのNumPy average()メソッド
PythonのNumPyモジュールには、データセットまたはリストにあるデータ項目の平均/平均を計算するための組み込み関数があります。
numpy.average()
メソッドは、入力リストの平均を計算するために使用されます。
例:
import numpy
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
lst_avg = numpy.average(inp_lst)
print("Average value of the list:\n")
print(lst_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))
出力:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
結論
したがって、この記事では、Pythonリストの平均を求めるためのさまざまな手法を明らかにし、理解しました。
参考文献
Source:
https://www.digitalocean.com/community/tutorials/average-of-list-in-python