python参考手册 Read

P28 复制

 1 a = [1,2,3,[1,2]]
 2 b = a
 3 b is a # True
 4 c = list[a] # shallow copy
 5 c is a # False
 6 c[3][0] = 100
 7 a # [1,2,3,[100,2]]
 8 import copy
 9 d = copy.deepcopy(a) # deep copy, recusion
10 d[3][0] = 99
11 a # [1,2,3,[100,2]]

P29 Python 中一切都是第一类的

Before

1 line = "Good, 100, 490.10"
2 foo = line.split(',')
3 
4 result = [str(foo[0]), int(foo[1]), float(foo[2])]
5 
6 print result

After

1 line = "Good, 100, 490.10"
2 foo = line.split(',')
3 t = [str, int, float]
4 
5 result = [ty(val) for ty,val in zip(t, foo)]
6 
7 print result

P90 生成器表达式

1 f = open("data.txt")
2 lines = (t.strip() for t in f)
3 
4 comments = (t for t in lines if t[0] == '#')
5 for c in comments:
6     print(c)

这样不会把整个文件读到内存中,提取注释时也是这样。

P63

等于运算符 == (测试x和y的值是否相等)

身份运算符 is (测试两个对象是否引用了内存中的同一个对象,可能 x==y 但是 x is not y)

P64 条件表达式

minValue = a if a <= b else b

先计算中间的条件 如果为 True 在对if语句左边的表达式求值,不然对else后面的表达式求值

 

posted @ 2014-08-07 15:20  mess4u  阅读(177)  评论(0编辑  收藏  举报