collectons module
from collections import OrderedDict # d = OrderedDict() # d['a'] = 1 # d['z'] = 2 # d['b'] = 3 # print(d) #ordered #d
['z'] = 0
#change #print(d)
default dictionary
Example: There are the following value sets [11, 22, 33, 44, 55, 66, 77, 88, 99, 90], save the value greater than 66 to the value of the first key, and save the value less than 66 to of the second key.
# dec = {} # dic.setdefault('k',[]) # print (Dec) # dec = {} # l = [11,22,33,44,55,66,77,88,99,90] # for i in l : # if i > 66: # if dic.get('k1'): # dic['k1'].append(i) # else: # dic['k1']=[i] # elif i < 66 : # if dic.get('k2'): # dic['k2'].append(i) # else: # dic ['to'] = [i] # print (Dec) # from collections import defaultdict # d = defaultdict(list) # l = [11,22,33,44,55,66,77,88,99,90] # for i in l: # if i > 66: # d['k1'].append(i) # elif i < 66: # d['k2'].append(i) # print(d) # from collections import defaultdict # d = defaultdict(lambda :5) # print(d[1]) # print(d) # from collections import Counter # c = Counter('abcdeabcdabcd') # print(c)
The default dictionary can either let its default value be a dictionary, list, set, or let its default value be a number, a string.
The biggest advantage of default dictionaries is that you will never get an error when you use a fetchable value.
The default dictionary is to set the default value for the value in the dictionary.
counter module
# from collections import Counter # c = Counter('abcdeabcdabcaba') # print(c)
collections module
# class Configparser: # def __init__(self,section,option): # self.section = section # self.option = option # def write(self,f): # f.write(self.section) # f = open('test','w') # config = Configparser('a','b') # config.write(f) # import configparser # configparser.ConfigParser.write()
item module
import time
time.time() Microsoft's time synchronization server
time
Calculate the time to execute the code time.time()
Let the program stop here for a while and sleep
Format of record time:
for people to see
for the machine
for calculation
time.sleep(set)
Delay the run for the specified time, in seconds
time.time()
Get current timestamp
# import time # timestamp time # print(time.time()) # Timestamp time # Accurate calculation of time difference #Format time # print(time.strftime('%Y-%m-%d %H:%M:%S'))# String formatting time # Record it for people to see # print(time.strftime('%y-%m-%d'))# string format time # print(time.strftime('%x %X'))# string format time # print(time.strftime('%c'))# string format time # structured time # print(time.localtime())# Local structured time # Middleware for converting timestamp time to formatted time # Simple calculation of the corresponding term # print(time.gmtime()) # UK structured time
Structured time is an intermediate value between formatted time and timestamp
example
2015-8-8 Timestamp time
# p = time.strptime('2015-8-8','%Y-%m-%d') # print(p) # print(time.mktime(p)) # print(time.time()-time.mktime(p)) # print(time.time()) # # ret = time.localtime (1500000000) # print (ret) # print(time.strftime('%Y-%m-%d %H:%M:%S',ret)) # ret = time.localtime (2000000000) # print (ret) # print(time.strftime('%Y-%m-%d %H:%M:%S',ret)) # ret = time.localtime (3000000000) # print (ret) # print(time.strftime('%Y-%m-%d %H:%M:%S',ret)) #2033 2065 32 # ret = time.localtime (0) # print (ret) # print(time.strftime('%Y-%m-%d %H:%M:%S',ret)) #1970 1 1 8 # # ret = time.gmtime (0) # print (ret) # print(time.strftime('%Y-%m-%d %H:%M:%S',ret)) #1970 1 1 0 # print(time.strftime('%c')) # print(time.ctime(1500000000)) # ret = time.localtime (2000000000) # print (ret) # print(time.asctime()) # print (time.asctime (ret))
sys module and os module
# sys.argv command line parameter list, the first element is the path of the program itself # sys.exit(n) Exit the program, exit(0) when exiting normally, exit sys.exit(1) in error # sys.version Get the version information of the Python interpreter # sys.path returns the search path of the module, using the value of the PYTHONPATH environment variable during initialization # sys.platform returns the operating system platform name
# import sys # print('*'*6) # sys.exit() #Exit the program, exit(0) when exiting normally, exit sys.exit(1) with error # print('-'*6)
# import sys # print(sys.version) # import sys # print(sys.platform) # import sys # ***** # print(sys.path)
RAM:
while the program is running
Start the interpreter to load a basic content, built-in function, built-in module --> in memory
# sys.path system path import # import os,sys # import sys # print(sys.argv) # The first item in the list is the path where the current file is located
# import sys # user = input('>>>') # pwd = input('>>>') # if user == 'alex' and pwd == '3714': # print('Login successful') # else: # sys.exit() # print('What I can do') # import sys # if sys.argv[1] == 'alex' and sys.argv[2] == '3714': # print('Login successful') # else: # sys.exit()
# import logging # num = int(input('>>>')) # logging.debug(num) # a = num*100 # logging.debug(num) # b = a - 10 # logging.debug(num) # c = b + 5 # print(c) # import sys # import logging # inp = sys.argv[1] if len(sys.argv) > 1 else 'WARNING' # logging.basicConfig(level=getattr(logging,inp)) # num = int(input('>>>')) # a = num * 100 # logging.debug(a) # b =a - 10 # logging.debug(b) # c = b + 5 # print(c)
rondom module (random) to shuffle the order
# print(random.random()) # 0-1 random decimals # print(random.uniform(1,4)) # print(random.randint(1,1000)) # contains 1,2 # print(random.randrange(1,2)) # does not contain 2 # print(random.randrange(1,20,2)) # contains all odd numbers # print(random.choice([1,'23',[4,5]])) # print(random.sample([1,'23',[4,5]],2)) # item=[1,3,5,7,9] # import random # random.shuffle(item) # print(item)