我不知道的python (补充)

1.原来swap is easy. Just use a,b=b,a
2.Catch errors rather than avoiding: Use try: except: expression is better
easier to ask forgiveness than permission.

3.Use map, filter expression:
def triple(x):
"""Returns 3 * x.Count: raises AttributeError if .Count missing."""
return 3 * x.Count

def check_count(x):
"""Returns 1 if x.Count exists and is greater than 3, 0 otherwise."""
try:
return x.Count > 3
except:
return 0

result = map(triple, filter(check_count, data))


4.zip('abc', [1,2,3]) == [('a',1),('b',2),('c',3)]
5. Method to do sum,multiply
from operator import add, mul
sum = reduce(add, data)
product = reduce(mul, data)

6. list sort reverse
list.sort()
list.reverse()
7. Use dictionaries (or sets) for searching, not lists. To find items in common between two lists, make the first into a dictionary and then look for items in the second in it.

评论

此博客中的热门博文

Embedded System interview Question

MicroKernel & Exokernel 操作系统未来可能的发展

中国城市房地产分析