# 简易Web Server,可以直接快速共享文件 python -m http.server # 脚本性能分析 python -m cProfile my_script.py # 列表辗平 import itertools a_list = [[1, 2], [3, 4], [5, 6]] print(list(itertools.chain(*a_list))) # Output: [1, 2, 3, 4, 5, 6] # 一行式的构造器,避免类初始化时大量重复的赋值语句 class A: def __init__(self,a,b,c,d,e,f): self.__dict__.update({k:v for k,v in locals().items()}) # 一行式斐波那契 fib = lambda x: x if x<=1 else fib(x-1) + fib(x-2) # 一行式快排 qsort = lambda L: [] if L==[] else qsort([x for x in L[1:] if x< L[0]]) + L[0:1] + qsort([x for x in L[1:] if x>=L[0]]) # CSV file to json python -c "import csv,json;print json.dumps(list(csv.reader(open('csv_file.csv'))))"
更多的一行式方法请参考 https://wiki.python.org/moin/Powerful%20Python%20One-Liners