파이썬 | 목록에서 요소의 발생 횟수를 계산합니다.

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에서 루프 사용하기
  • 목록 이해 사용
  • 열거 함수 사용
  • 개수() 사용
  • 카운터() 사용
  • countOf() 사용
  • 사용 사전 이해
  • Pandas 라이브러리 사용

Python에서 루프를 사용하여 Python 카운트 발생

필요한 요소가 목록에서 발견되면 계속 증가하는 카운터를 유지합니다.

파이썬3




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

산출

8 has occurred 5 times 

List Comprehension을 사용한 Python 개수 발생

이 예에서는 목록 이해 Python 목록에서 요소의 모든 발생을 계산합니다.

파이썬3




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 

열거 함수를 사용한 Python 계산

이 예에서는 열거 함수 Python 목록에서 요소의 모든 발생을 계산합니다.

파이썬3




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()를 사용하여 요소의 발생 횟수를 계산합니다.

아이디어는 목록 메소드 개수() 발생 횟수를 계산합니다.

파이썬3




# 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()를 사용하여 목록에 있는 요소의 발생 횟수를 계산합니다.

카운터 메소드는 키-값 쌍으로 모든 요소가 발생하는 사전을 반환합니다. 여기서 키는 요소이고 값은 해당 요소가 발생한 횟수입니다.

파이썬3




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의 발생 횟수 계산 노래하다 카운트오브()

Operator.countOf()는 a에서 b가 나타나는 횟수를 계산하는 데 사용됩니다. 값의 발생 횟수를 계산합니다. 값이 발생한 횟수를 반환합니다.

파이썬3




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 사전 이해 사용하기

파이썬은 허용합니다 사전 이해 . 간단한 표현식을 사용하여 사전을 만들 수 있습니다. 사전 이해는 {key: value for (key, value) in iterable} 형식을 취합니다.

파이썬3




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

산출

1 

Pandas 라이브러리 사용

Pandas Series.value_counts() 함수는 고유 값의 개수를 포함하는 시리즈를 반환합니다. 결과 개체는 첫 번째 요소가 가장 자주 발생하는 요소가 되도록 내림차순으로 정렬됩니다.

파이썬3




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