[what] Collections Module In Python


.

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
dequelist-like container with fast appends and pops on either end
ChainMapdict-like class for creating a single view of multiple mappings
Counterdict subclass for counting hashable objects
OrderedDictdict subclass that remembers the order entries were added
defaultdictdict subclass that calls a factory function to supply missing values
UserDictwrapper around dictionary objects for easier dict subclassing
UserListwrapper around list objects for easier list subclassing
UserStringwrapper 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:

Post a Comment

0 Comments