Please enable JavaScript.
Coggle requires JavaScript to display documents.
12-Advanced Python Modules - Coggle Diagram
12-Advanced
Python Modules
00-Collection
Module
collection module
defaultdict
Defaultdict is a sub-class of the dict class that returns a dictionary-like object
The functionality of both dictionaries and defualtdict are almost same except for the fact that defualtdict never raises a KeyError
Defaultdict is a container like dictionaries present in the module collections
provides a default value for the key that does not exists
To overcome the issue when the
KeyError
is raised, Python introduces another dictionary like container known as
Defaultdict
which is present inside the collections module
syntax
d = defaultdict(default_factory)
default_factory: A function returning the default value for the dictionary defined. If this argument is absent then the dictionary raises a KeyError
missing
()
provide the default value for the dictionary
takes default_factory as an argument
is basically called by the
getitem
() method of the dict class when the requested key is not found
getitem
() raises or return the value returned by the
missing
(). method
Counter
how to
use
Counter()
with list
lst = [1,2,2,2,2,3,3,3,1,2,1,12,3,2,32,1,21,1,223,1]
Counter(lst)
Counter({1: 6, 2: 6, 3: 4, 12: 1, 21: 1, 32: 1, 223: 1})
Counter with strings
from collections import Counter
Counter with words
in a sentence
words = s.split()
Counter(words)
common pattern when
using Counter() object
dict(c)
convert to a regular dictionary
c.items()
convert to a list of (elem, cnt) pairs
[(a,b)]
set(c)
convert to a set list with all unique elements on set format
Counter(dict(list_of_pairs))
convert from a list of (elem, cnt) pairs
list(c)
lst = [1,2,2,2,2,3,3,3,1,2,1,12,3,2,32,1,21,1,223,1]
c = Counter(lst)
print(list(c))
list all unique element by list format
result: [1,2,3,12,32,223,21]
list unique elements
c.most_common(value)
most_common(2)
most_common()
sort the dictionary and give the numbers of most common elements
return the most common elements from Counter list
c.clear() # reset all counts
c += Counter()
remove zero and negative counts
sum(c.values()) # total of all counts
keys: element
value: the counts of the object
Counter is a dict subclass which helps count hashable objects
namedtuple
implements specialized container data types providing alternatives to Python’s general purpose built-in containers
01-Datetime
-Module
The arguments to initialize a time instance are optional, but the default of 0
time
A time instance only holds values of time, and not a date associated with the time
t = datetime(10,2,4)
t.tzinfo
daylight saving time: the practice with shifting their clocks one hour forward in the spring and an hour back in the fall
It finds its applications in situations such as the conversion of local time to UTC or to account for daylight saving time
some
methods
utcoffset(self, dt)
time aware
objects
def utcoffset(self, dt):
return self.stdoffset + self.dst(dt)
fixed
offset
def utcoff(self, dt):
return constant
returns the difference btw time in datetime object & UTC
Naive and Aware
datetime objects
Aware datetime
objects
contains the time zone
Naive datetime
objects
not contain any information on time zone
cannot be instantiated directly
We can create a timestamp by specifying datetime.time(hour,minute,second,microsecond)
extract time information from the datetime module
include time zone information
Dates
datetime.datetime.now()
do not have any information about the time zones (
naive datetime object)
just uses the current system time
In some situations, the time zone details may be needed. In such cases the tzinfo abstract class is used
Creating Date
Objects
use the datetime() class (constructor) of the datetime module
t = datetime.datetime(2021,7,1)
The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone)
strftime()
takes one parameter, format, to specify the format of the returned string
format codes at
link
formatting date objects into readable strings
A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects
Times have attributes for hour, minute, second, and microsecond
Arithmetic
Time values are represented with the time class
to help deal with timestamps