カウンター クラスは、 コレクション Python3のモジュール。 Collections モジュールは、特殊なコンテナ データ型をユーザーに提供するため、辞書、リスト、タプルなどの Python の汎用ビルトインの代替手段となります。
カウンター ハッシュ可能なオブジェクトをカウントするために使用されるサブクラスです。呼び出されると反復可能オブジェクトのハッシュ テーブルが暗黙的に作成されます。
要素() これは Counter クラスの関数の 1 つで、Counter オブジェクトに対して呼び出されると、Counter オブジェクト内のすべての既知の要素の itertool を返します。
パラメーター : パラメータを取りません
戻り値の型: Counter オブジェクト内の正のカウントを持つすべての要素の itertool を返します。
エラーと例外:
-> 特定のデータコンテナではなく itertool を返すため、直接印刷するとガベージ値が出力されます。
-> 項目のカウントがすでに Counter オブジェクトで初期化されている場合、ゼロと負の値を持つ項目は無視されます。
コード #1: 単純なデータコンテナでの elements() の動作
Python3
# import counter class from collections module> from> collections>import> Counter> # Creation of a Counter Class object using> # string as an iterable data container> x>=> Counter(>'geeksforgeeks'>)> # printing the elements of counter object> for> i>in> x.elements():> >print> ( i, end>=> ' '>)> |
>
画像をCSSで揃える
>
出力:
g g e e e e k k s s f o r>
リストを反復可能なデータ コンテナとして使用して、Counter クラス オブジェクトを作成することもできます。
Python3
# import counter class from collections module> from> collections>import> Counter> #Creating a Counter class object using list as an iterable data container> a>=> [>12>,>3>,>4>,>3>,>5>,>11>,>12>,>6>,>7>]> x>=> Counter(a)> #directly printing whole x> print>(x)> #We can also use .keys() and .values() methods to access Counter class object> for> i>in> x.keys():> >print>(i,>':'>, x[i])> > #We can also make a list of keys and values of x> x_keys>=> list>(x.keys())> x_values>=> list>(x.values())> print>(x_keys)> print>(x_values)> |
>
C++の文字列
>
出力:
Counter({12: 2, 3: 2, 4: 1, 5: 1, 11: 1, 6: 1, 7: 1}) 12 : 2 3 : 2 4 : 1 5 : 1 11 : 1 6 : 1 7 : 1 [12, 3, 4, 5, 11, 6, 7] [2, 2, 1, 1, 1, 1, 1]> コード #2: さまざまなデータ コンテナを持つさまざまなカウンター オブジェクトの要素
Python3
# import counter class from collections module> from> collections>import> Counter> # Creation of a Counter Class object using> # a string as an iterable data container> # Example - 1> a>=> Counter(>'geeksforgeeks'>)> # Elements of counter object> for> i>in> a.elements():> >print> ( i, end>=> ' '>)> print>()> > # Example - 2> b>=> Counter({>'geeks'> :>4>,>'for'> :>1>,> >'gfg'> :>2>,>'python'> :>3>})> for> i>in> b.elements():> >print> ( i, end>=> ' '>)> print>()> # Example - 3> c>=> Counter([>1>,>2>,>21>,>12>,>2>,>44>,>5>,> >13>,>15>,>5>,>19>,>21>,>5>])> for> i>in> c.elements():> >print> ( i, end>=> ' '>)> print>()> > # Example - 4> d>=> Counter( a>=> 2>, b>=> 3>, c>=> 6>, d>=> 1>, e>=> 5>)> for> i>in> d.elements():> >print> ( i, end>=> ' '>)> |
>
>
出力:
g g e e e e k k s s f o r geeks geeks geeks geeks for gfg gfg python python python 1 2 2 21 21 12 44 5 5 5 13 15 19 a a b b b c c c c c c d e e e e e>
コード #3: 直接印刷したときに elements() が何を返すかを示すには
Python3
# import Counter from collections> from> collections>import> Counter> # creating a raw data-set> x>=> Counter (>'geeksforgeeks'>)> # will return a itertools chain object> # which is basically a pseudo iterable container whose> # elements can be used when called with a iterable tool> print>(x.elements())> |
スクリプトを実行可能にする
>
>
出力:
itertools.chain object at 0x037209F0>
コード #4: Counter 内の項目のカウントが負の値またはゼロで初期化された場合。
Python3
# import Counter from collections> from> collections>import> Counter> # creating a raw data-set using keyword arguments> x>=> Counter (a>=> 2>, x>=> 3>, b>=> 3>, z>=> 1>, y>=> 5>, c>=> 0>, d>=> ->3>)> # printing out the elements> for> i>in> x.elements():> >print>(>'% s : % s'> %> (i, x[i]), end>=>'
'>)> |
>
>
出力:
a : 2 a : 2 x : 3 x : 3 x : 3 b : 3 b : 3 b : 3 z : 1 y : 5 y : 5 y : 5 y : 5 y : 5>
注記: 出力から、1 未満の値を持つ項目は elements() によって省略されることが推測できます。
アプリケーション:
Counter オブジェクトとその関数は、大量のデータを処理するために集合的に使用されます。 Counter() は、それによって呼び出されたデータ コンテナのハッシュ マップを作成することがわかります。これは、要素を手動で処理するよりも非常に便利です。これは非常に高度な処理と機能を備えたツールの 1 つであり、広範囲のデータを処理することもできます。