Analyze Data/Python Libraries

collections-Counter, most_common, FreqDist, defaultdict

Naranjito 2022. 3. 4. 23:53
  • Counter

 

Remove duplicates and return the frequency.

Counter('mississippi')

>>>
Counter({'m': 1, 'i': 4, 's': 4, 'p': 2})
Counter(list('mississippi'))

>>>
Counter({'m': 1, 'i': 4, 's': 4, 'p': 2})

 

  • most_common

 

most_common(n)

Return a list of the n most common elements and their counts.

Counter('abracadabra').most_common(3)

>>>
[('a', 5), ('b', 2), ('r', 2)]

 

  • FreqDist

 

Frequency Distribution, it records the number of times each outome.

preprocessed_sentences=[['barber', 'person'], ['barber', 'good', 'person'], ['barber', 'huge', 'person'], ['knew', 'secret'], ['secret', 'kept', 'huge', 'secret'], ['huge', 'secret'], ['barber', 'kept', 'word'], ['barber', 'kept', 'word'], ['barber', 'kept', 'secret'], ['keeping', 'keeping', 'huge', 'secret', 'driving', 'barber', 'crazy'], ['barber', 'went', 'huge', 'mountain']]
FreqDist(np.hstack(preprocessed_sentences))

>>>
FreqDist({'barber': 8, 'secret': 6, 'huge': 5, 'kept': 4, 'person': 3, 'word': 2, 'keeping': 2, 'good': 1, 'knew': 1, 'driving': 1, ...})

 

  • defaultdict

 

Grouping a sequence of key-value pairs into a dictionary of lists using list.

from collections import defaultdict

s=[('yellow',1), ('blue',2), ('yellow',3), ('blue',4), ('red',1)]
d=defaultdict(list)
for k,v in s:
    d[k].append(v)
print(d)

>>>

defaultdict(<class 'list'>, {'yellow': [1, 3], 'blue': [2, 4], 'red': [1]})

shape of collections.defaultdict(int) is { : }

 

  • deque

 

Double-ended queue. List-like container with fast appends and pops on either end.

from collections import deque
queue.popleft()

>>>
'eric'
ordinals=deque(['first','second','third'])
ordinals.rotate(-2)
>>>

deque(['third', 'first', 'second'])

ordinals.rotate(-1)
>>>

deque(['first', 'second', 'third'])

 

https://excelsior-cjh.tistory.com/96