Zhancha python library standard library

Zhan tea python Library (I) standard library In this section, we will explain the common standard library of python, whi...


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.

Catalog

1.time Library 1.1 get current time
# Get local time time.localtime() # Get UTC universal time time.gmtime() # Returns the local time string time.ctime()
1.2 time stamps and timers
# Returns the number of seconds since the era, recording sleep time.time() # Randomly select a time point, record the number of seconds between the current time and the time point, and record sleep time.perf_counter() # Select a time point at will, record the number of seconds between the current time and the time point, and do not record sleep time.process_time() # 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
time.sleep()
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) randrange(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) random() # Generate random floating-point numbers between [a, b] uniform(a, b)
2.4 sequence function
# Returns an element at random from a sequence type choice(seq) # 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 suffle(seq) # 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) print(p)
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 count_str.most_common(n) # Element unfolding count_str.elements()
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 d.append("f") # Increase on the left side d.appendleft("a") # Right side deletion d.pop() # Left side deletion d.popleft()
4.itertools Library 4.1 permutation and combination iterator

(1) Cartesian product

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

(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): print(i)

(3) Combinations

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

(4) Combinations with replacement

import itertools for i in itertools.combinations_with_replacement("ABCD", 3): print(i)
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") print(i)

(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"): print(i) for i in itertools.ziplongest("ABC", "012345", fillvalue = "?"): print(i)
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

itertools.count(10)

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

itertools.count("ABC")

(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): print(i)
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]): print(i)

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

for i in itertools.enumerate("ABC", start = 1): print(i) `` (3)Grouping 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))

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

5 February 2020, 05:42 | Views: 5605

Add new comment

For adding a comment, please log in
or create account

0 comments