数组数据过滤
list2 = [l for l in list if l > 3]
对等代码:
list2 = []for l in list: if l > 3: list2.append(l)
条件判断
ret = (a > b and a or b)
对等代码:
if a > b: ret = aelse: ret = b
数据结构
from collections import namedtuplecls = namedtuple('cls', 'a, b, c')c = cls(a='a', b='b', c='c')print c.a # c.a只能读, 不能修改, 类似情况多用于函数返回值需要, 避免返回值能用c[0]等不利于维护的方式返回