python全栈开发 * 26知识点汇总 * 180709
Posted j-7-h-2-f-7
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python全栈开发 * 26知识点汇总 * 180709相关的知识,希望对你有一定的参考价值。
26 logging collections random 模块
一.logging
低配:日志不能写入文件与显示同时进行
1.只写入文件:
import logging
logger=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 logging
logger=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 logging
logger=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 logging
logger=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")
二.collections
1.namedtuple
from collections import namedtuple
point = namedtuple("point",["x","y"])
p=point(10,30) #point(x=10, y=30)
print(p)
2.deque 双向列队
from collections import deque
q=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.OrderedDict
dic = {}
dic[‘name‘] = ‘alex‘
dic[‘age‘] = ‘1000‘
dic[‘sex‘] = ‘男‘
print(dic)
from collections import OrderedDict
od=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 OrderedDict
od1 = 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 defaultdict
l1 = [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]})
练习2
dic1 = {} #---> dic1={1:5,2:5,3:5.....20:5}
方法一:
for i in range(1,21):
dic1[i]=5
print(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.Counter
from collections import Counter
c=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‘})
三.random
1.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的顺序
以上是关于python全栈开发 * 26知识点汇总 * 180709的主要内容,如果未能解决你的问题,请参考以下文章
python全栈开发 * 22 知识点汇总 * 180703
python全栈开发 * 18 面向对象知识点汇总 * 180530