Zhancha python library standard library

Zhan tea python Library (I) standard library
In this section, we will explain the common standard library of python, which mainly describes some common library files and machine learning related libraries such as numpy, pandas, etc.


1.time Library

1.1 get current time
# Get local time
# Get UTC universal time
# Returns the local time string
1.2 time stamps and timers
# Returns the number of seconds since the era, recording sleep
# Randomly select a time point, record the number of seconds between the current time and the time point, and record sleep
# Select a time point at will, record the number of seconds between the current time and the time point, and do not record sleep
# Perf counter is a little more accurate than time
1.3 format
# time.strftime custom format output
lctime = time.localtime()
time.strftime("%Y-%m-%d %H:%M:%S", lctime)
1.4 dormancy

2.random Library

Random library is widely used. python can generate various pseudo-random numbers through random library.

2.1 random seeds

The same random seed will generate the same random number. If the random seed is not set, the current time of the system is the default value. The setting method is as follows:

2.2 generating random integers
# Generating random integers between [a, b]
randint(a, b)
# Generate a random integer between [0, a)
# Generate a random integer with step as step between [a, b]
randrange(a, b, step)
2.3 generate random floating point number
# Generate random floating-point numbers between [0.0, 1.0)
# Generate random floating-point numbers between [a, b]
uniform(a, b)
2.4 sequence function
# Returns an element at random from a sequence type
# Repeat sampling k times for the sequence type, and you can set the weight
choice(seq, wrights=None, k)
# Randomly arrange the elements in the sequence type to return the scrambled sequence
# Randomly select k elements from seq type to return as list type
sample(pop, k)
2.5 probability distribution
# To produce a random number conforming to Gaussian distribution
gauss(mean, std)

3.collections Library

3.1 named tuples

To build a new tuple subclass, define it as follows: typename means tuple name, and field name is domain name

# collection.namedtuple(typename, field_name, *, rename=False, defaults=None, module=None)
Point = collection.namedtuple("Point", ["x", "y"])
p = Point(x=1, y=2)
3.2 counter tools

Counter counter is a subclass of dictionary, which can be used as follows

s = "abcdefg"
count_str = Counter(s)
colors = ["red", "blue", "green"]
count_color = Counter(colors)
# Provide n elements and count with the highest frequency
# Element unfolding
3.3 two way queue

deque is a two-way queue. The list access is very fast, and the insertion and deletion of a single list are very slow, because it needs to be realized by moving the element location, especially at the beginning of the list. The two-way queue can efficiently and quickly add and delete elements on both sides of the queue

from collections import deque
d = deque("cde")
# Increase on the right side
# Increase on the left side
# Right side deletion
# Left side deletion

4.itertools Library

4.1 permutation and combination iterator

(1) Cartesian product

import itertools
for i in itertools.product("ABC", "01"):
for i in itertools.product("ABC",  repeat=3):

(2) Permutations

import itertools
# 3 is the length of the arrangement, which refers to the arrangement of three elements
for i in itertools.permutations("ABCD", 3):

(3) Combinations

import itertools
# 3 is the length of the combination
for i in itertools.combinations("ABCD", 3):

(4) Combinations with replacement

import itertools
for i in itertools.combinations_with_replacement("ABCD", 3):
4.2 zipper

(1) Short zip
When the zipper length is inconsistent, execute to the shortest place to stop

for i in zip("ABC", "123", "XYZ")

(2) Long zip
When the zipper length is inconsistent, the default element is replaced by None or the specified character

for i in itertools.ziplongest("ABC", "012345"):
for i in itertools.ziplongest("ABC", "012345", fillvalue = "?"):

4.3 infinite iterator

(1) count
count(start=0, step=1) means to create an iterator, which starts from start and returns the value of uniform interval


(2) circulation
cycle(iterable) means to create an iterator, return all elements in iterable, and repeat infinitely


(3) repetition
repeat(object, [, times]) means to create an iterator and repeat the object continuously. By default, infinite loops can be set, and the number of cycles can be set

for i in itertools.repeat(10, 3):

5. other

5.1 chains

(1) chains
chain(iterable) means to concatenate a set of iterative objects to form a larger iterator

for i in itertools.chain("ABC", [1, 2, 3]):

(2) enumeration
enumerate(iterable, start = 0) means to produce a tuple of two elements

for i in itertools.enumerate("ABC", start = 1):
groupby(iterable, key = None)Means to create an iterator according to key Specified method, return itertools Continuous keys and groups in

for key, group in itertools.groupby("ABCDEFG"):
print(key, list(group))

Published 41 original articles, won praise 37, visited 20000+
Private letter follow

Tags: Python

Posted on Wed, 05 Feb 2020 05:42:39 -0500 by mazsolt