파이썬 | 카운터 개체 | 강요()
카운터 클래스는 다음과 함께 제공되는 특별한 유형의 객체 데이터 세트입니다. 컬렉션 Python3의 모듈입니다. 컬렉션 모듈은 사용자에게 특수 컨테이너 데이터 유형을 제공하므로 사전, 목록 및 튜플과 같은 Python의 범용 내장 기능에 대한 대안을 제공합니다.
카운터 해시 가능한 개체 수를 계산하는 데 사용되는 하위 클래스입니다. 호출될 때 iterable의 해시 테이블을 암시적으로 생성합니다.
강요() Counter 클래스의 함수 중 하나입니다. Counter 개체에서 호출되면 Counter 개체에 알려진 모든 요소의 itertool이 반환됩니다.
매개변수: 매개변수를 사용하지 않습니다.
반환 유형: Counter 객체에서 양수 개수를 가진 모든 요소에 대해 itertool을 반환합니다.
오류 및 예외:
-> 특정 데이터 컨테이너가 아닌 itertool을 반환하기 때문에 직접 인쇄하면 쓰레기 값을 인쇄합니다.
-> 항목 개수가 Counter 개체에서 이미 초기화된 경우 0과 음수 값을 가진 항목은 무시됩니다.
코드 #1: 간단한 데이터 컨테이너에서 elements() 작업
파이썬3
# 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> => ' '> )> |
산출:
g g e e e e k k s s f o r
목록을 반복 가능한 데이터 컨테이너로 사용하여 Counter 클래스 객체를 만들 수도 있습니다.
파이썬3
# 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)> |
산출:
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: 다양한 데이터 컨테이너가 있는 다양한 카운터 개체의 요소
파이썬3
# 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()가 반환하는 내용을 보여주기 위해
파이썬3
# 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의 항목 개수가 음수 또는 0으로 초기화되는 경우입니다.
파이썬3
# 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()가 호출된 데이터 컨테이너에 대한 해시 맵을 생성하는 것을 볼 수 있으며 이는 요소를 수동으로 처리하는 것보다 매우 유용합니다. 이는 매우 뛰어난 처리 및 기능 도구 중 하나이며 광범위한 데이터에서도 작동할 수 있습니다.