Last active
August 29, 2015 14:03
-
-
Save iziang/468e2448ae66ce605ee6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#从列表的特定位置迭代,比如从第五项迭代到末尾,可以用下面的方法,当然也可以把列表从指定位置分片后再迭代 | |
for i in list_inst[4:]: | |
process | |
#可以对可迭代对象进行解包操作,类似于下面这样的形式,只要两边的参数个数匹配即可 | |
m = [1, 2, 3] | |
p, q, r = m | |
#自定义异常类的定义,触发,处理 | |
#!/usr/bin/python | |
# Filename: raising.py | |
class ShortInputException(Exception): | |
'''A user-defined exception class.''' | |
def __init__(self, length, atleast): | |
Exception.__init__(self) | |
self.length = length | |
self.atleast = atleast | |
def tell(self): | |
print "i'm a exception" | |
try: | |
s = raw_input('Enter something --> ') | |
if len(s) < 3: | |
raise ShortInputException(len(s), 3) | |
# Other work can continue as usual here | |
except EOFError: | |
print '\nWhy did you do an EOF on me?' | |
except ShortInputException, x: | |
print 'ShortInputException: The input was of length %d, \ | |
was expecting at least %d' % (x.length, x.atleast) | |
x.tell() | |
else: | |
print 'No exception was raised.' | |
#python没有switch语句,因为可以用字典完全实现switch的功能 | |
#得到当前安装的python 所支持的socket选项列表 | |
[ x for x in dir(socket) if x startswith("SO_") ] | |
#生成可变长度的密码(注意choice和join的用法,连接成字符串的方法) | |
from random import choice | |
def makepass(length=10, chars=string.letters+string.digits): | |
return ''.join([choice(chars) for i in range(length)]) | |
********************************************************************************************************** | |
#条件函数,根据某个变量的不同,采用不同的函数 | |
myfunc = var and (lambda s: " ".join(s.split())) or (lambda s: s) | |
#根据var的不同取值,myfunc分别取不同的函数 | |
********************************************************************************************************** | |
#isinstance函数的第二个参数可以是元组,把想匹配的类型都写进去 | |
a = 3.12 | |
isinstance(a, (int,float)) | |
********************************************************************************************************** | |
#getattr函数可以返回默认值 | |
class Company: | |
pass | |
inst = Company() | |
getattr(inst, 'zion') #会出错,instance has no attribute 'foo' | |
getattr(inst, 'zion', 'Netease') #会返回‘Netease’ | |
********************************************************************************************************** | |
#你是一名体育老师,在某次课距离下课还有五分钟时,你决定搞一个游戏。此时有100名学生在上课。游戏的规则是: | |
#你首先说出三个不同的特殊数,要求必须是个位数,比如3、5、7。 | |
#让所有学生拍成一队,然后按顺序报数。 | |
#学生报数时,如果所报数字是第一个特殊数(3)的倍数,那么不能说该数字,而要说Fizz;如果所报数字是第二个特殊数(5)的倍数,那么要说Buzz;如果所报数字是第三个特殊数(7)的倍数,那么要说Whizz。 | |
#学生报数时,如果所报数字同时是两个特殊数的倍数情况下,也要特殊处理,比如第一个特殊数和第二个特殊数的倍数,那么不能说该数字,而是要说FizzBuzz, 以此类推。如果同时是三个特殊数的倍数,那么要说FizzBuzzWhizz。 | |
#学生报数时,如果所报数字包含了第一个特殊数,那么也不能说该数字,而是要说相应的单词,比如本例中第一个特殊数是3,那么要报13的同学应该说Fizz。如果数字中包含了第一个特殊数,那么忽略规则3和规则4,比如要报35的同学只报Fizz,不报BuzzWhizz。 | |
#现在,我们需要你完成一个程序来模拟这个游戏,它首先接受3个特殊数,然后输出100名学生应该报数的数或单词 | |
f = lambda a,b,c:['Fizz'*(str(a) in str(x)) or 'Fizz'*(x%a==0) + 'Buzz'*(x%b==0) + 'Whizz'*(x%c==0) or x for x in range(1,101) if 0<a<b<c<10] | |
print f(3,5,7) | |
********************************************************************************************************** | |
#把10个问号用逗号分隔开,一般用在批量生成占位符 | |
','.join('?'*10) | |
#上边的例子适用于只有一个字符的情况,join里面的参数是字符串,join会在每两个字符中间插入逗号 | |
#如果我们有两个字符,比如%s,还按照上边的写法的话就不对了,这时,join里面的参数可以用列表的形式,join会在每两个列表元素之间插入逗号 | |
','.join(['%s']*10) | |
'?'*10 => '?,?,?,?,?,?,?,?,?,?' | |
['%s']*10 => ['%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'] | |
********************************************************************************************************** | |
#注意,wrapper函数中重新定义的函数参数要与被装饰的函数参数个数一致 | |
class Coordinate(object): | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
def __repr__(self): | |
return "Coord: " + str(self.__dict__) | |
def wrapper(func): | |
def checker(a, b): | |
if a.x < 0 or a.y < 0: | |
a = Coordinate(a.x if a.x > 0 else 0, a.y if a.y > 0 else 0) | |
if b.x < 0 or b.y < 0: | |
b = Coordinate(b.x if b.x > 0 else 0, b.y if b.y > 0 else 0) | |
ret = func(a, b) | |
if ret.x < 0 or ret.y < 0: | |
ret = Coordinate(ret.x if ret.x > 0 else 0, ret.y if ret.y > 0 else 0) | |
return ret | |
return checker | |
@wrapper | |
def add(a, b): | |
return Coordinate(a.x + b.x, a.y + b.y) | |
one = Coordinate(100, 200) | |
two = Coordinate(300, 200) | |
three = Coordinate(300, 200) | |
print add(one, two) | |
********************************************************************************************************** | |
#python中无法对字典排序 | |
#但是有时候我们想要按照键的大小,输出字典的元素,或者按照键值的大小输出 | |
#可以采用变通的方式,得到一个(key,value)对的列表,方法就是用sorted函数 | |
sorted(iterable[, cmp[, key[, reverse]]]) | |
#按照键的大小排序,生成key,value对列表 | |
sorted(mydict.items(), key=lambda x:x[0], reverse=True) | |
#按照j键值的大小排序,生成key,value对列表 | |
sorted(mydict.items(), key=lambda x:x[1], reverse=True) | |
#还有另外一种根据键值排序的方法,就是先构造一个字典,每个元素的键值对与要排序字典的键值对相反,然后根据键排序即可 | |
m = [[value,key] for key,value in mydict.items()] | |
#然后,根据m中的键排序就可以了 | |
#上边的这些排序方法,实际上思想是一样的,先把字典中的元素分离出来,存到列表中,然后对列表排序 | |
********************************************************************************************************** | |
#检查输入是否为数字 | |
try: | |
val = int(your_input) | |
except ValueError: | |
print 'not a number' | |
********************************************************************************************************** | |
********************************************************************************************************** | |
********************************************************************************************************** | |
********************************************************************************************************** | |
********************************************************************************************************** | |
********************************************************************************************************** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment