.
This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict , list , set , tuple .
namedtuple() | factory function for creating tuple subclasses with named fields |
deque | list-like container with fast appends and pops on either end |
ChainMap | dict-like class for creating a single view of multiple mappings |
Counter | dict subclass for counting hashable objects |
OrderedDict | dict subclass that remembers the order entries were added |
defaultdict | dict subclass that calls a factory function to supply missing values |
UserDict | wrapper around dictionary objects for easier dict subclassing |
UserList | wrapper around list objects for easier list subclassing |
UserString | wrapper around string objects for easier string subclassing |
.
# Using Counter object from Collections Module
# to get word frequencies
# and common words
from collections import Counter
print ("\n====================\n")
textSource='''
Twinkle, twinkle, little star.
How I wonder what you are.
Up above the world so high.
Like a diamond in the sky.
'''
print("Source text:\n{}".format(str(textSource)))
print ("\n====================\n")
# replace comma with no char
# replace new line with no char
# replace dot with space char
# lower all char
# split by space char
# sort
listWord=sorted(textSource
.replace(",","")
.replace("\n","")
.replace("."," ")
.lower()
.split())
print("Preprocessed text tokens:\n{}"
.format(str(listWord)))
print ("\n====================\n")
print ("Word frequencies (using Counter object):")
# using Counter object
cntr=Counter(listWord)
print(cntr)
More examples:

0 Comments