- 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'])
'Analyze Data > Python Libraries' 카테고리의 다른 글
numpy-ndim, ravel, permutation, clip, subtract (0) | 2022.05.10 |
---|---|
regular expression (0) | 2022.04.26 |
pandas-5. json_normalize (0) | 2021.10.25 |
mlxtend-TransactionEncoder, association_rules (0) | 2021.06.23 |
pandas-4. read_csv, unique, to_csv, file upload, file download (0) | 2021.06.22 |