logo

Python | Contor obiecte | elemente()

Tejghea clasa este un tip special de set de date obiect furnizat cu colecții modul în Python3. Modulul de colecții oferă utilizatorului tipuri de date de containere specializate, oferind astfel o alternativă la integratele de uz general Python, cum ar fi dicționare, liste și tupluri.

Tejghea este o subclasă care este folosită pentru a număra obiectele hashabile. Acesta creează implicit un tabel hash al unui iterabil atunci când este invocat.

elemente() este una dintre funcțiile clasei Counter, atunci când este invocată pe obiectul Counter, va returna un iertool cu ​​toate elementele cunoscute din obiectul Counter.



Parametri: Nu ia niciun parametru
Tip returnare: Returnează un itertool pentru toate elementele cu număr pozitiv din obiectul Counter
Erori și excepții:
-> Acesta va imprima o valoare de gunoi atunci când este imprimat direct, deoarece returnează un itertool, nu un anumit container de date.
-> Dacă numărul unui articol este deja inițializat în obiectul Counter, atunci le va ignora pe cele cu valori zero și negative.

Codul #1: Lucrarea elementelor() pe un container de date simplu

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>=> ' '>)>

>

cum se apelează o metodă în java

>

Ieșire:

g g e e e e k k s s f o r>

De asemenea, putem crea obiectul clasei Counter folosind o listă ca container de date iterabil.

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)>

>

>

Ieșire:

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]>

Codul #2: Elemente pe o varietate de obiecte contrare cu diferite containere de date

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>=> ' '>)>

>

>

Ieșire:

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>

Codul #3: Pentru a demonstra ce elemente returnează () când este tipărită direct

Python3


cum se returnează o matrice în java



# 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())>

>

>

Ieșire:

itertools.chain object at 0x037209F0>

Codul #4: Când numărul unui articol din Counter este inițializat cu valori negative sau zero.

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>=>' '>)>

Logica de ordinul 1

>

>

Ieșire:

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>

Notă: Din rezultat putem deduce că elementele cu valori mai mici de 1 sunt omise de elemente ().

Aplicatii:
Obiectul contor împreună cu funcțiile sale sunt utilizate în mod colectiv pentru procesarea unor cantități uriașe de date. Putem vedea că Counter() creează o hartă hash pentru containerul de date invocat cu acesta, ceea ce este foarte util decât prin procesarea manuală a elementelor. Este unul dintre instrumentele foarte înalte de procesare și funcționare și poate funcționa chiar și cu o gamă largă de date.