摘要: 本文系官方文档翻译之作,不当之处,敬请原谅!range()函数如果需要遍历一个数字序列,可以使用内置的range函数。该函数会生成等差序列。123range(5)# 范围[0, 5)range(5,10)# 范围[5, 10)range(0,10,3)# [0, 3, 6, 9]若要依据索引迭代序列,可以使用range()和len()123a =['Mary','had','a','little'... 阅读全文
posted @ 2016-05-17 09:39 指上弹兵赵小括 阅读(253) 评论(0) 推荐(0) 编辑
摘要: /** * Input an array of positive integers, arrange the integers to form new digits, * and output the smallest digit among all the new ones. * Input Ex 阅读全文
posted @ 2016-02-23 19:35 指上弹兵赵小括 阅读(244) 评论(0) 推荐(0) 编辑
摘要: 使用@property 阅读: 20616 在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻辑。为了限制score的范围,可以通过一个set_score()方法... 阅读全文
posted @ 2015-12-16 11:22 指上弹兵赵小括 阅读(413) 评论(0) 推荐(0) 编辑
摘要: 1、__str__和__repr__如果要把一个类的实例变成 str,就需要实现特殊方法__str__():class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender def __str__(self): r... 阅读全文
posted @ 2015-12-16 11:14 指上弹兵赵小括 阅读(294) 评论(0) 推荐(0) 编辑
摘要: 获取对象信息阅读: 31336当我们拿到一个对象的引用时,如何知道这个对象是什么类型、有哪些方法呢?使用type()首先,我们来判断对象类型,使用type()函数:基本类型都可以用type()判断:>>> type(123) >>> type('str') >>> type(None) 如果一个变量指向函数或者类,也可以用type()判断:>>> type(abs) >>> type(a)... 阅读全文
posted @ 2015-12-16 10:53 指上弹兵赵小括 阅读(218) 评论(0) 推荐(0) 编辑
摘要: 1、编写无参数的decoratorPython的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数。使用 decorator 用Python提供的 @ 语法,这样可以避免手动编写 f = decorate(f) 这样的代码。def log(f): def fn(x): print 'call ' + f.__name__ + '().... 阅读全文
posted @ 2015-12-15 16:37 指上弹兵赵小括 阅读(277) 评论(0) 推荐(0) 编辑
摘要: 慕课网 http://www.imooc.com/1、变量可以指向一个函数(函数名相当于一个变量)12345import mathdef add(x, y, fun): return fun(x) + fun(y) print(add(25, 9, math.sqrt))2、map函数【python 2】Help on built-in function map in module __bu... 阅读全文
posted @ 2015-12-15 15:35 指上弹兵赵小括 阅读(196) 评论(0) 推荐(0) 编辑
摘要: 1、使用enumerate函数L = ['Adam', 'Lisa', 'Bart', 'Paul']for index, name in enumerate(L): print index + 1, '-', name.lower()2、使用zip函数for index, name in zip(range(1, len(L)+1), L): print index, '-', na... 阅读全文
posted @ 2015-12-14 21:37 指上弹兵赵小括 阅读(213) 评论(0) 推荐(0) 编辑
摘要: 1、列表推导1234567891011numbers = [i for i in range(10) if i % 2 == 0]print(numbers)seq = ["one", "two", "three"]for i, element in enumerate(seq):print(i, ... 阅读全文
posted @ 2015-12-14 21:34 指上弹兵赵小括 阅读(193) 评论(0) 推荐(0) 编辑
摘要: 1、生成窗口最大值数组有一个整型数组arr和一个大小为w的窗口从数组的最左边滑到最右边,窗口每次向右边滑一个位置。例如,数组为[4,3,5,4,3,3,6,7],窗口大小为3时:[4 3 5] 4 3 3 6 7 窗口中最大值为54 [3 5 4] 3 3 6 7 窗口中最大值为54 3 [5 4 ... 阅读全文
posted @ 2015-10-09 20:59 指上弹兵赵小括 阅读(632) 评论(0) 推荐(0) 编辑