logo

パイソン |リスト内の要素の出現をカウントします。

Python のリストと数値 x が与えられた場合、指定されたリスト内の x の出現数を数えます。

例:



 Input: lst = [15, 6, 7, 10, 12, 20, 10, 28, 10], x = 10 Output: 3 Explanation:  10 appears three times in given list. Input: lst = [8, 6, 8, 10, 8, 20, 10, 8, 8], x = 16 Output: 0 Explanation: 16 appears zero times in given list.>

Python リスト内の項目の出現数をカウントする

以下は、Python リスト内の要素の出現をすべてカウントできるメソッドです。

  • Python でループを使用する
  • リスト内包表記の使用
  • 列挙関数の使用
  • count() の使用
  • Counter() の使用
  • countOf() の使用
  • 使用する 辞書の理解力
  • Pandas のライブラリを使用する

Python Python のループを使用して出現回数をカウントする

必要な要素がリスト内に見つかった場合に増加し続けるカウンターを保持します。

Python3








# Python code to count the number of occurrences> def> countX(lst, x):> >count>=> 0> >for> ele>in> lst:> >if> (ele>=>=> x):> >count>=> count>+> 1> >return> count> # Driver Code> lst>=> [>8>,>6>,>8>,>10>,>8>,>20>,>10>,>8>,>8>]> x>=> 8> print>(>'{} has occurred {} times'>.>format>(x,> >countX(lst, x)))>

>

c

>

出力

8 has occurred 5 times>

リスト内包表記を使用した Python の出現数のカウント

この例では、 リストの内包表記 Python リスト内の要素の出現をすべてカウントします。

Python3




l>=> [>1>,>1>,>2>,>2>,>2>,>3>,>3>,>4>,>4>,>5>,>5>]> ele>=>1> x>=>[i>for> i>in> l>if> i>=>=>ele]> print>(>'the element'>,ele,>'occurs'>,>len>(x),>'times'>)>

>

>

出力

the element 1 occurs 2 times>

Enumerate 関数を使用した Python のカウント

この例では、 列挙関数 Python リスト内の要素の出現をすべてカウントします。

Python3




l>=> [>1>,>1>,>2>,>2>,>2>,>3>,>3>,>4>,>4>,>5>,>5>]> ele>=>1> x>=>[i>for> j,i>in> enumerate>(l)>if> i>=>=>ele]> print>(>'the element'>,ele,>'occurs'>,>len>(x),>'times'>)>

>

>

出力

the element 1 occurs 2 times>

count() を使用して要素の出現をカウントする

アイデアは、 リストメソッド count() 出現回数をカウントします。

Python3




# Python code to count the number of occurrences> def> countX(lst, x):> >return> lst.count(x)> # Driver Code> lst>=> [>8>,>6>,>8>,>10>,>8>,>20>,>10>,>8>,>8>]> x>=> 8> print>(>'{} has occurred {} times'>.>format>(x,> >countX(lst, x)))>

>

>

出力

8 has occurred 5 times>

Python Counter() を使用してリスト内の要素の出現をカウントします。

c counter メソッドは、すべての要素の出現をキーと値のペアとして含む辞書を返します。ここで、キーは要素、値はその要素が出現した回数です。

Python3




from> collections>import> Counter> # declaring the list> l>=> [>1>,>1>,>2>,>2>,>3>,>3>,>4>,>4>,>5>,>5>]> # driver program> x>=> 3> d>=> Counter(l)> print>(>'{} has occurred {} times'>.>format>(x, d[x]))>

>

>

出力

3 has occurred 2 times>

要素 u の出現をカウントします。 歌う countOf()

Operator.countOf( ) は、a 内の b の出現数をカウントするために使用されます。 value の出現回数をカウントします。 value の出現回数の Count を返します。

Python3




import> operator as op> # declaring the list> l>=> [>1>,>1>,>2>,>2>,>2>,>3>,>3>,>4>,>4>,>5>,>5>]> # driver program> x>=> 2> print>(f>'{x} has occurred {op.countOf(l, x)} times'>)>

>

>

出力

2 has occurred 3 times>

Python 辞書内包表記の使用

Python では可能 辞書の理解 。簡単な式を使用して辞書を作成できます。辞書内包表記は、{key: value for (key, value) in iterable} の形式を取ります。

Python3




lis>=> [>'a'>,>'d'>,>'d'>,>'c'>,>'a'>,>'b'>,>'b'>,>'a'>,>'c'>,>'d'>,>'e'>]> occurrence>=> {item: lis.count(item)>for> item>in> lis}> print>(occurrence.get(>'e'>))>

>

Java文字列の長さ
>

出力

1>

パンダライブラリの使用

Pandas Series.value_counts() 関数は、一意の値の数を含む Series を返します。結果のオブジェクトは降順になるため、最初の要素が最も頻繁に出現する要素になります。

Python3




import> pandas as pd> # declaring the list> l>=> [>1>,>1>,>2>,>2>,>2>,>3>,>3>,>4>,>4>,>5>,>5>]> count>=> pd.Series(l).value_counts()> print>(>'Element Count'>)> print>(count)>

>

>

出力:

Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int64>