#----------------------------------------------------------------------- halve_evens_only = lambda nums: map(lambda i: i/2,\ filter(lambda i: not i%2, nums)) #----------------------------------------------------------------------- def halve_evens_only(nums): return [i/2 for i in nums if not i % 2]
a, b = b, a
a = [1,2,3,4,5] >>> a[::2] # 以步长为2的增量迭代整个list对象 [1,3,5]
>>> a[::-1] [5,4,3,2,1]
def function(x, l=[]): # 不要这么干
def function(x, l=None): # 更好的一种方式
if l is None:
l = []
d = {1: "1", 2: "2", 3: "3"}
for key, val in d.items() # 调用items()后会构建一个完整的list对象
for key, val in d.iteritems() # 只有在迭代时每请求一次才生成一个值
if type(s) == type(""): ...
if type(seq) == list or \
type(seq) == tuple: ...
if isinstance(s, basestring): ... if isinstance(seq, (list, tuple)): ...
>>> a=u'aaaa' >>> print isinstance(a, basestring) True >>> print isinstance(a, str) False
freqs = {}
for c in "abracadabra":
try:
freqs[c] += 1
except:
freqs[c] = 1
freqs = {}
for c in "abracadabra":
freqs[c] = freqs.get(c, 0) + 1
from collections import defaultdict
freqs = defaultdict(int)
for c in "abracadabra":
freqs[c] += 1
namedtuple() # 用指定的域创建元组子类的工厂函数 deque # 类似list的容器,快速追加以及删除在序列的两端 Counter # 统计哈希表的dict子类 OrderedDict # 记录实体添加顺序的dict子类 defaultdict # 调用工厂方法为key提供缺省值的dict子类
__eq__(self, other) # 定义相等操作的行为, ==. __ne__(self, other) # 定义不相等操作的行为, !=. __lt__(self, other) #定义小于操作的行为, <. __gt__(self, other) #定义不大于操作的行为, >. __le__(self, other) #定义小于等于操作的行为, <=. __ge__(self, other) #定义大于等于操作的行为, >=.
x = 3 if (y == 1) else 2
x = 3 if (y == 1) else 2 if (y == -1) else 1
(func1 if y == 1 else func2)(arg1, arg2)
x = (class1 if y == 1 else class2)(arg1, arg2)
class MyClass(object):
def __init__(self, a, b, c, d):
self.a, self.b, self.c, self.d = a, b, c, d
def __getitem__(self, item):
return getattr(self, item)
x = MyClass(10, 12, 22, 14)
def __getitem__(self, item):
if item is Ellipsis:
return [self.a, self.b, self.c, self.d]
else:
return getattr(self, item)
>>> x = MyClass(11, 34, 23, 12) >>> x[...] [11, 34, 23, 12]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有