Givet en liste i Python og et tal x, tæl antallet af forekomster af x i den givne liste.
Eksempler:
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.>
Tæl forekomster af genstande i Python-listen
Nedenfor er de metoder, hvormed vi kan tælle alle forekomster af et element i en Python-liste.
- Brug af en loop i Python
- Brug af listeforståelse
- Brug af enumerate-funktion
- Brug af count()
- Brug af tæller()
- Brug af countOf()
- Ved brug af ordbogsforståelse
- Brug af Pandas bibliotek
Python Count forekomster ved hjælp af en løkke i Python
Vi holder en tæller, der bliver ved med at stige, hvis det ønskede element findes på listen.
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)))> |
>
>Produktion
8 has occurred 5 times>
Pythontæller forekomster ved hjælp af listeforståelse
I dette eksempel bruger vi listeforståelse at tælle alle forekomster af et element i en Python-liste.
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'> )> |
>
boto3
>Produktion
the element 1 occurs 2 times>
Pythontælling ved hjælp af Enumerate-funktionen
I dette eksempel bruger vi opregne funktion at tælle alle forekomster af et element i en Python-liste.
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'> )> |
>
>Produktion
the element 1 occurs 2 times>
Tæl forekomster af et element ved hjælp af count()
Ideen er at bruge liste metode count() at tælle antallet af forekomster.
Python3
gimp fjern vandmærke
# 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)))> |
>
>Produktion
8 has occurred 5 times>
Python Count forekomster af et element i en liste ved hjælp af Counter()
Tællermetoden returnerer en ordbog med forekomster af alle elementer som et nøgle-værdi-par, hvor nøglen er elementet, og værdien er antallet af gange, det element er opstået.
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]))> |
>
>Produktion
3 has occurred 2 times>
Tæl forekomster af et element u synge countOf()
Operator.countOf( ) bruges til at tælle antallet af forekomster af b i a. Det tæller antallet af forekomster af værdi. Det returnerer antallet af et antal forekomster af værdi.
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'> )> |
>
git pull origin master
>Produktion
2 has occurred 3 times>
Brug af Python Dictionary Comprehension
Python tillader ordbogsforståelser . Vi kan lave ordbøger ved hjælp af simple udtryk. En ordbogsforståelse har formen {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'> ))> |
>
>Produktion
1>
Brug af Pandas-biblioteket
Pandas Series.value_counts()-funktion returnerer en serie, der indeholder antallet af unikke værdier. Det resulterende objekt vil være i faldende rækkefølge, så det første element er det mest hyppigt forekommende element.
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)> |
>
>
Produktion:
Element Count 2 3 1 2 3 2 4 2 5 2 dtype: int64>