statisticsモジュールのmode関数を使うことで、リストやタプル内の最頻値を取得できます。
statistics.mode関数
![statistics mode 1](https://sunagitsune.com/wp-content/uploads/2022/10/statistics-mode-1.jpg)
mode関数を使うには、最初にstatistics組み込みモジュールをimportする必要があります。
#python
import statistics
list = [1,2,3,4,2,3,4,3,4,4]
md = statistics.mode(list)
print(md)
statistics.modeの引数にリスト/タプルを指定することで、
![statistics mode 2](https://sunagitsune.com/wp-content/uploads/2022/10/statistics-mode-2.jpg)
リスト内で最もよく出る値を取得できます。
![statistics mode 3](https://sunagitsune.com/wp-content/uploads/2022/10/statistics-mode-3.jpg)
最頻値が複数ある場合、最初に見付かった最頻値が取得されます。
![](https://sunagitsune.com/wp-content/uploads/2022/05/laptop-g2651bd324_1280-160x90.png)
Python操作方法、関数・メソッドリファレンス
使用用途別にまとめたPythonリファレンスページです。
初期準備、基本概念
ダウンロード、インストール、起動
ShellとEditor、保存、実行
保存したPythonの起動
コメント、docstring、行またぎ
コメント、...
コメント