num = [1, 4, -5, 10, -7, 2, 3, -1] filtered_and_squared = [] for number in num: if number > 0: filtered_and_squared.append(number ** 2) print filtered_and_squared # [1, 16, 100, 4, 9]
num = [1, 4, -5, 10, -7, 2, 3, -1] filtered_and_squared = map(lambda x: x ** 2, filter(lambda x: x > 0, num)) print filtered_and_squared # [1, 16, 100, 4, 9]
num = [1, 4, -5, 10, -7, 2, 3, -1] filtered_and_squared = [ x**2 for x in num if x > 0] print filtered_and_squared # [1, 16, 100, 4, 9]
num = [1, 4, -5, 10, -7, 2, 3, -1] filtered_and_squared = ( x**2 for x in num if x > 0 ) print filtered_and_squared # <generator object <genexpr> at 0x00583E18> for item in filtered_and_squared: print item # 1, 16, 100 4,9
num = [1, 4, -5, 10, -7, 2, 3, -1] def square_generator(optional_parameter): return (x ** 2 for x in num if x > optional_parameter) print square_generator(0) # <generator object <genexpr> at 0x004E6418> # Option I for k in square_generator(0): print k # 1, 16, 100, 4, 9 # Option II g = list(square_generator(0)) print g # [1, 16, 100, 4, 9]
alist = ['a1', 'a2', 'a3'] blist = ['1', '2', '3'] for a, b in zip(alist, blist): print a, b # a1 1 # a2 2 # a3 3
import os
def tree(top):
for path, names, fnames in os.walk(top):
for fname in fnames:
yield os.path.join(path, fname)
for name in tree('C:\Users\XXX\Downloads\Test'):
print name
def timethis(func): ''' Decorator that reports the execution time. ''' pass @timethis def countdown(n): while n > 0: n -= 1
import time
from functools import wraps
def timethis(func):
'''
Decorator that reports the execution time.
'''
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(func.__name__, end-start)
return result
return wrapper
@timethis
def countdown(n):
while n > 0:
n -= 1
countdown(100000)
# ('countdown', 0.006999969482421875)
@timethis def countdown(n):
def countdown(n): ... countdown = timethis(countdown)
@decorator
def function():
print("inside function")
class decorator(object):
def __init__(self, f):
print("inside decorator.__init__()")
f() # Prove that function definition has completed
def __call__(self):
print("inside decorator.__call__()")
@decorator
def function():
print("inside function()")
print("Finished decorating function()")
function()
# inside decorator.__init__()
# inside function()
# Finished decorating function()
# inside decorator.__call__()
def decorator(func):
def modify(*args, **kwargs):
variable = kwargs.pop('variable', None)
print variable
x,y=func(*args, **kwargs)
return x,y
return modify
@decorator
def func(a,b):
print a**2,b**2
return a**2,b**2
func(a=4, b=5, variable="hi")
func(a=4, b=5)
# hi
# 16 25
# None
# 16 25
import time
class demo:
def __init__(self, label):
self.label = label
def __enter__(self):
self.start = time.time()
def __exit__(self, exc_ty, exc_val, exc_tb):
end = time.time()
print('{}: {}'.format(self.label, end - self.start))
import time
class demo:
def __init__(self, label):
self.label = label
def __enter__(self):
self.start = time.time()
def __exit__(self, exc_ty, exc_val, exc_tb):
end = time.time()
print('{}: {}'.format(self.label, end - self.start))
with demo('counting'):
n = 10000000
while n > 0:
n -= 1
# counting: 1.36000013351
from contextlib import contextmanager
import time
@contextmanager
def demo(label):
start = time.time()
try:
yield
finally:
end = time.time()
print('{}: {}'.format(label, end - start))
with demo('counting'):
n = 10000000
while n > 0:
n -= 1
# counting: 1.32399988174
class Celsius(object): def __init__(self, value=0.0): self.value = float(value) def __get__(self, instance, owner): return self.value def __set__(self, instance, value): self.value = float(value) class Temperature(object): celsius = Celsius() temp=Temperature() temp.celsius #calls Celsius.__get__
import weakref class lazyattribute(object): def __init__(self, f): self.data = weakref.WeakKeyDictionary() self.f = f def __get__(self, obj, cls): if obj not in self.data: self.data[obj] = self.f(obj) return self.data[obj] class Foo(object): @lazyattribute def bar(self): print "Being lazy" return 42 f = Foo() print f.bar # Being lazy # 42 print f.bar # 42
class demo(object):
pass
obj = demo()
print "Class of obj is {0}".format(obj.__class__)
print "Class of obj is {0}".format(demo.__class__)
# Class of obj is <class '__main__.demo'>
# Class of obj is <type 'type'>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有