Python
 Computer >> コンピューター >  >> プログラミング >> Python

Pythonでプログラムを作成して、シリーズで最も繰り返される要素を見つけます


次のシリーズがあると仮定します

Series is:
0    1
1    22
2    3
3    4
4    22
5    5
6    22

そして、最も繰り返される要素の結果は、

Repeated element is: 22

解決策

これを解決するために、以下のアプローチに従います

  • シリーズを定義する

  • 初期カウントを0に設定し、max_count値をシリーズの最初の要素値データとして設定します[0]

count = 0
max_count = data[0]
  • 系列データにアクセスするためのforループを作成し、frequency_countをl.count(i)

    として設定します。
for i in data:
   frequency_count = l.count(i)
  • 条件をmax_count値と比較するように設定し、条件がtrueの場合、countをfrequency_countに割り当て、max_countをseriespresent要素に変更します。最後に、max_countを出力します。以下に定義されています

if(frequency_count > max_count):
   count = frequency_count
   max_count = i
print("Repeated element is:", max_count)

理解を深めるために、以下の実装を見てみましょう-

import pandas as pd
l = [1,22,3,4,22,5,22]
data = pd.Series(l)
print("Series is:\n", data)
count = 0
max_count = data[0]
for i in data:
   frequency_count = l.count(i)
   if(frequency_count > max_count):
      count = frequency_count
      max_count = i
print("Repeated element is:", max_count)

出力

Series is:
0    1
1    22
2    3
3    4
4    22
5    5
6    22
dtype: int64
Repeated element is: 22

  1. 辞書で2番目に大きい値を見つけるPythonプログラム

    この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 − 2つの整数が与えられているので、辞書に2番目に大きい値を出力する必要があります それでは、以下の実装の概念を見てみましょう- アプローチ1-負のインデックスによるsorted()関数の使用 例 #input example_dict ={"tutor":3, "tutorials":15, "point":9,"tutorialspoint":19} # sorting the given list and get the

  2. 配列内の最大の要素を見つけるPythonプログラム

    この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −配列が与えられたので、配列の最大要素を計算する必要があります。 ここでは、ループ全体をトラバースして最大の要素を計算し、要素を取得するブルートフォースアプローチを使用します。 以下の実装を観察できます。 例 # largest function def largest(arr,n):    #maximum element    max = arr[0]    # traverse the whole loop    for