logo

Pitone | Oggetti contatore | elementi()

Contatore La classe è un tipo speciale di set di dati oggetto fornito con collezioni modulo in Python3. Il modulo Collections fornisce all'utente tipi di dati contenitore specializzati, fornendo quindi un'alternativa ai built-in generici di Python come dizionari, elenchi e tuple.

Contatore è una sottoclasse utilizzata per contare gli oggetti hashable. Crea implicitamente una tabella hash di un iterabile quando richiamato.

elementi() è una delle funzioni della classe Counter, quando invocata sull'oggetto Counter restituirà un itertool di tutti gli elementi conosciuti nell'oggetto Counter.



Parametri: Non accetta alcun parametro
Tipo di reso: Restituisce un itertool per tutti gli elementi con conteggio positivo nell'oggetto Counter
Errori ed eccezioni:
-> Stamperà un valore spazzatura quando stampato direttamente perché restituisce un itertool, non un contenitore di dati specifico.
-> Se il conteggio di un elemento è già inizializzato nell'oggetto Contatore, ignorerà quelli con valori zero e negativi.

Codice n. 1: Funzionamento di elements() su un semplice contenitore di dati

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

>

imbottitura np

>

Produzione:

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

Possiamo anche creare un oggetto di classe Counter utilizzando un elenco come contenitore di dati iterabile.

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

>

flusso di filtro Java

>

Produzione:

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

Codice n. 2: Elementi su una varietà di oggetti contatore con diversi contenitori di dati

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

>

>

Produzione:

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>

Codice n. 3: Per dimostrare cosa restituisce elements() quando viene stampato direttamente

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

>

>

Produzione:

itertools.chain object at 0x037209F0>

Codice n. 4: Quando il conteggio di un elemento in Counter viene inizializzato con valori negativi o 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>=>' '>)>

>

pulsante centrale nei css

>

Produzione:

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>

Nota: Possiamo dedurre dall'output che gli elementi con valori inferiori a 1 vengono omessi da elements().

Applicazioni:
L'oggetto contatore insieme alle sue funzioni vengono utilizzati collettivamente per elaborare enormi quantità di dati. Possiamo vedere che Counter() crea una mappa hash per il contenitore di dati invocato con esso che è molto utile rispetto all'elaborazione manuale degli elementi. È uno degli strumenti di elaborazione e funzionamento di altissimo livello e può funzionare anche con un'ampia gamma di dati.