-- encoding: utf-8 --
"""
@File : test.py
@Time : 2020/4/13 15:05
@Author : wanghaochen
"""
二叉树排序, 小的放左边,大的放右边, 如果右边已经有了, 比较子节点的值, 大的放左边, 小的放右边
每一个值看做一个对象, 有左右之分
class Btree:
def init(self, node):
self.node = node
self.left = None
self.right = None
self.root = Btree(self.l[0])
def makeTree(self,cos,node):
res_list = []
if cos < node.node and node.left == None:
node.left = Btree(cos)
elif cos < node.node and node.left != None:
node.left = node.makeTree(cos,node.left)
elif cos > node.node and node.left == None:
node.right = Btree(cos)
elif cos < node.node and node.right != None:
node.right = node.makeTree(cos, node.right)
res_list.append(node.left.node)
def get_data(self):pass
l = [1, 2, 3, 4, 5]
map 映射 sorted 排序 filter 过滤
使用lambda返回的是列表
ll = {'dddd': 5, 'ddd': 8}
def fun(dic):
for key in dic:
dic[key] += 1
return dic
print sorted(ll,reverse=True)
print map(lambda x:x,ll)
def func(x):
a = 0
b = 1
while b < x:
b, a = a + b, b
yield a
用yield返回值的函数是生成器 用括号括起来的列表生成式 是生成器
迭代器 实现了iter方法, 可以用next()放法取值,列表,字典,字符串,元组是可迭代对象,不是迭代器
迭代器表示的是数据流,可以被next()函数不断调用返回下一个值,节约内存
可以用for循环的是iterable, 可以被next()调用的是迭代器,用yield方法返回值的是生成器
print list(func(50))
from collections import OrderedDict, Counter
dic = {'name':"da",'age':55}
dic = OrderedDict(dic)
# sorted 中 key 是根据什么来排序
sorted(dic,key=lambda i:i[0])
dddd = 'ddagdgdagdafdsa'
print Counter(dddd)
print dic
dd = "ddd"
s = 'eeee'
l1 = filter(lambda x:x==2,l)
print s.join(dd)
a = [1, 5, 1, 2, 3]
b = [1,2,3]
c = [1,2,3]
res = [i for i in zip(a,b,c)]
print res
udpate student set name = "dd" where id = 55
dd = []
for i in range(len(a)):
print min(a)
dd.append(min(a))
a.remove(min(a))
remove方法,移除第一找到的元素
a.remove(1)
print a
class Student(object):
_ins = None
# def init(self):
def new(cls, *args, **kwargs):
if not cls._ins:
_ins = object.new(cls)
return cls._ins
else:
return cls._ins
st = Student()
dd = Student()
print st is dd
a = 55.365
print '%.02f' % a
垃圾回收机制
引用计数
+1 变量创建, 被引用, 被传参
-1 变量被显式删除 , 别名重新赋值, 离开作用域[比如函数内部作用域] 所在容器被销毁
分代清除
分三代, 创建时间越短, 被检查清理的频率越高
import gc
# gc模块,设置日志,检查哪代进行回收,设置回收频率,查看当前计数器, 设置阙值触发垃圾回收检查
gc.set_debug(gc.DEBUG_LEAK)
s =88
print gc.get_count()
gc.collect(2)
print(gc.garbage)
print(gc.collect())
print(gc.garbage)
print gc.get_count()
class A(object):
def dd(self):
print 88
class B(A):
def dd(self):
super(B,self).dd()
print 999
print B().dd()
301 永久重定向 302 临时重定向 400 请求语法错误或参数错误 401 认证失败 403 拒绝
dic ={'name':55 , 'age':8}
# print dic.pop('age')
# del dic['age']
纸牌模型
import random
from collections import namedtuple
Card = namedtuple('Card', ['rank', 'suit'])
class FrenchDesk(object):
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
_cards = None
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, position):
return self._cards[position]
# def __iter__(self):
# return self._cards
def random(self):
return random.choice(self._cards)
deck = FrenchDesk()
suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)
def spades_high(card):
rank_value = FrenchDesk.ranks.index(card.rank)
return rank_value * len(suit_values) + suit_values[card.suit]
print deck.random()
print Card(rank='10', suit='spades') in deck
deck_new = sorted(deck,key = spades_high)
向量模型
hypot函数用来求绝对值 hypot(3,4) = 5
%r 可以重现它所代表的对象
from math import hypot
print(hypot(3, 4))
class Vector(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# 当打印时,如果没有__str__方法,会优先调用__repr__方法
def __repr__(self):
return 'Vector(%r,%r)' % (self.x, self.y)
def __abs__(self):
return hypot(self.x, self.y)
def __bool__(self): pass
python2中,变量泄露,i=9 python3中,i=555,局部变量
i = 555
u = [i for i in range(10)]
print(i) # i=9
笛卡尔积
colors = ['black', 'white']
sizes = ['S', 'M', 'L']
group = [(color, size) for color in colors for size in sizes]
import os
print os.path
创建字典
l = ('a', 'b')
l1 = ('1', '2')
print(dict(zip(l, l1)))
namedtuple
_asdict()方法可以转化成有序字典orderdict
_make()方法生成实例
from collections import namedtuple
ff = namedtuple('ff', 'name,age')
xh = ff('xh', '15')
print ff._fields
data = ('xl', 25)
print ff._make(data)
print xh._asdict()
元组方法展示
e = (1, 2)
print id(e)
e1 = (3, 4)
e += e1
print id(e)
列表方法, 在原来的基础上扩容,列表还是之前的列表
e.remove(1) 删除匹配到的第一个元素
e.clear() 删除所有元素
切片生成的新列表
e = [1, 2]
print(id(e))
e1 = [3, 4]
e += e1
e2 = e[0:1]
print(id(e))
print(id(e2))