26 logging collections random 模块 一.logging低配:日志不能写入文件与显示同时进行 1.只写入文件:import logginglogger=logging.getLogger() #创建一个对象fn=logging.FileHandler("高配.log",encoding="utf-8",mode="w")logger.addHandler(fn)logging.debug("debug message")logging.info("info message")logging.warning("warning message")logging.error("error message")logging.critical("critical message")2.写入文件 显示同时进行import logginglogger=logging.getLogger() #创建一个对象fn=logging.FileHandler("高配.log",encoding="utf-8",mode="w")sh=logging.StreamHandler()logger.addHandler(fn)logger.addHandler(sh)logging.debug("debug message")logging.info("info message")logging.warning("warning message")logging.error("error message")logging.critical("critical message")3.设置显示模式import logginglogger=logging.getLogger()fh=logging.FileHandler("高配.log",encoding="utf-8",mode="w")sh=logging.StreamHandler()formatter=logging.Formatter("%(asctime)s-%(name)s-%(levelname)s-%(message)s")logger.addHandler(fh)logger.addHandler(sh)sh.setFormatter(formatter)fh.setFormatter(formatter)logging.debug("debug message")logging.info("info message")logging.warning("warning message")logging.error("error message")logging.critical("critical message")4.设置等级import logginglogger=logging.getLogger()fh=logging.FileHandler("高配.log",encoding="utf-8",mode="w")sh=logging.StreamHandler()formatter=logging.Formatter("%(asctime)s-%(name)s-%(levelname)s-%(message)s")logger.setLevel(logging.DEBUG) # 没有它显示和写入文件默认从warning开始logger.addHandler(fh)logger.addHandler(sh)sh.setFormatter(formatter)fh.setFormatter(formatter)sh.setLevel(logging.DEBUG)fh.setLevel(logging.INFO)logging.debug("debug message")logging.info("info message")logging.warning("warning message")logging.error("error message")logging.critical("critical message")
二.collections1.namedtuplefrom collections import namedtuplepoint = namedtuple("point",["x","y"])p=point(10,30) #point(x=10, y=30)print(p)2.deque 双向列队from collections import dequeq=deque(["a","b","c","d","e","f"])增加q.append(666)q.append(888)q.appendleft(111)q.appendleft(222) #deque([222, 111, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, 666, 888])print(q)删除q1=deque(["a","b","c","d","e","f"])q1.pop() # deque([‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘])q1.pop() # deque([‘a‘, ‘b‘, ‘c‘, ‘d‘])q1.popleft() # deque([‘b‘, ‘c‘, ‘d‘])q1.popleft() # deque([‘c‘, ‘d‘])print(q1) 补充: queue 先进先出 fifo原则 栈 :先进后出3.OrderedDictdic = {}dic[‘name‘] = ‘alex‘dic[‘age‘] = ‘1000‘dic[‘sex‘] = ‘男‘print(dic)from collections import OrderedDictod=OrderedDict()od["name"]="alex"od["age"]="1000"print(od) # OrderedDict([(‘name‘, ‘alex‘), (‘age‘, ‘1000‘)])d = dict([(‘a‘, 1), (‘b‘, 2), (‘c‘, 3)])print(d) # {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}from collections import OrderedDictod1 = OrderedDict([(‘a‘, 1), (‘c‘, 3), (‘b‘, 2),])print(od1) # OrderedDict([(‘a‘, 1), (‘c‘, 3), (‘b‘, 2)]) 4.defaultdict练习方法一l1 = [11,22,33,44,55,77,88,99,90]dic = {}for i in l1: if i<66: if "key1" not in dic: dic["key1"]=[] dic["key1"].append(i) else: if "key2" not in dic: dic["key2"]=[] dic["key2"].append(i)print(dic) # {‘key1‘: [11, 22, 33, 44, 55], ‘key2‘: [77, 88, 99, 90]}方法二:from collections import defaultdictl1 = [11,22,33,44,55,77,88,99,90]my_dict=defaultdict(list)my_dict["k1"]my_dict["k2"]print(my_dict) #defaultdict(<class ‘list‘>, {‘k1‘: [], ‘k2‘: []})for v in l1: if v < 66: my_dict["k1"].append(v) else: my_dict["k2"].append(v)print(my_dict) # defaultdict(<class ‘list‘>, {‘k1‘: [11, 22, 33, 44, 55], ‘k2‘: [77, 88, 99, 90]})练习2dic1 = {} #---> dic1={1:5,2:5,3:5.....20:5}方法一:for i in range(1,21): dic1[i]=5print(dic1)方法二:dict={x:5 for x in range (1,21)}print(dict)方法三:dict1=defaultdict(lambda :5)for i in range(1,21): dict1[i]print(dict1)方法四dic2 = dict.fromkeys(range(1,21),5)fromkeys 第一个参数中每一个拿出来和第二个参数组织成键值对 (神坑请注意:生成出来的键指向的value是同一个对象,改变其中一个,另一个也会跟着改变.)print(dic2) 5.Counterfrom collections import Counterc=Counter("gdhhhfffddggghhhfff")d=Counter([1,2,2,2,2,3,3,3,3,4,4,4,4])e=Counter((6,6,6,6,6,8,8,8,8,8,8,))b=Counter({"name":"alex","name":"eve","name":"mary"})print(c) # Counter({‘h‘: 6, ‘f‘: 6, ‘g‘: 4, ‘d‘: 3}) 以字典的形式返回print(d) # Counter({2: 4, 3: 4, 4: 4, 1: 1})print(e) # Counter({8: 6, 6: 5})print(b) # Counter({‘name‘: ‘mary‘})三.random1.random(1,2) #0到1之间的小数2.uniform(1.3) # 大于1 小于3的小数3.randint(1,5) # 1=<,=<5的整数4.randrange(1,10,2) # 1到10之间的奇数 顾头不顾尾5.choice([1,"23",[4,5]) # 括号里面必须是有索引的数据类型6.sample([1,2,3,4,5,6,],2) #列表元素任意两个组合.7.shuffle(item) # 打乱item的顺序
原文地址:https://www.cnblogs.com/J-7-H-2-F-7/p/9288115.html
时间: 2024-11-02 21:01:16