pythonでリスト内の最頻値(最も数の多い値)を取得する方法/statistics.mode関数の使い方

IT

statisticsモジュールmode関数を使うことで、リストやタプル内の最頻値を取得できます。

スポンサーリンク

statistics.mode関数

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の引数にリスト/タプルを指定することで、
リスト内で最もよく出る値を取得できます。
最頻値が複数ある場合、最初に見付かった最頻値が取得されます。
Python操作方法、関数・メソッドリファレンス
使用用途別にまとめたPythonリファレンスページです。 初期準備、基本概念 ダウンロード、インストール、起動 ShellとEditor、保存、実行 保存したPythonの起動 コメント、docstring、行またぎ コメント、...

コメント

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny

モバイルバージョンを終了