流畅的python
老板来颗糖
这个作者很懒,什么都没留下…
展开
-
【流畅的Python】01-用namedtuple方法构建简单类
namedtuple ()是 collections库中的方法,也叫具名元组,是元组的升级版namedtuple的实例和tuple的实例消耗的内存一样多。自Python2.6 开始,namedtuple加入到Python中,用以构建只有少数属性但是没有方法的对象,比如数据库条目。import collections"""实现一个简单类"""# 定义简单类Role # 两种方法# ..原创 2018-10-14 21:06:44 · 252 阅读 · 0 评论 -
【流畅的python】03-创建字典的几种方式
python中创建字典的多种方式>>> # 1. 用键值表的方式创建字典>>> a = dict(one=1, two=2, three=3)>>> a{'three': 3, 'two': 2, 'one': 1}>>> # 2. 最常用的文字表达式>>> b = {"one原创 2018-10-26 12:18:51 · 242 阅读 · 0 评论 -
【流畅的python】04- 统计字符串中每个字符出现的次数
如题,思考出了四种方法,写法越来越pythonic1.# 1 for:for:message='统计字符串中每个字符出现的次数'count = {}n = 0message_set = set(message)for i in message_set: # print(i) for j in message: if i == j: ...原创 2018-10-27 01:00:30 · 821 阅读 · 0 评论 -
【流畅的python】02-用 bisect 来管理已排序的序列
原创《流畅的python》bisect 模块包含两个主要函数, bisect 和 insort ,两个函数都利用二分查找算法来在有序序列中查找或插入元素。并且是稳定排序在有序序列中用 bisect 查找某个元素的插入位置import bisectimport sysHAYSTACK = [1, 4, 5, 6, 8, 12, 15, 20, 21, 23, 23, 26, 29, ...原创 2018-10-24 22:04:02 · 302 阅读 · 0 评论