def find(seq, el) :
pos = bisect(seq, el)
if pos == 0 or ( pos == len(seq) and seq[-1] != el ) :
return -1
return pos - 1
bisect.insort(list, element)
seq = ['a', 'a', 'b']
res = []
for i in seq:
if i not in res:
res.append(i)
seq = ['a', 'a', 'b'] res = set(seq)
#使用profile stats工具进行性能分析
>>> from pbp.scripts.profiler import profile, stats
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3),
... ('blue', 4), ('red', 1)]
>>> @profile('defaultdict')
... def faster():
... d = defaultdict(list)
... for k, v in s:
... d[k].append(v)
...
>>> @profile('dict')
... def slower():
... d = {}
... for k, v in s:
... d.setdefault(k, []).append(v)
...
>>> slower(); faster()
Optimization: Solutions
[ 306 ]
>>> stats['dict']
{'stones': 16.587882671716077, 'memory': 396,
'time': 0.35166311264038086}
>>> stats['defaultdict']
{'stones': 6.5733464259021686, 'memory': 552,
'time': 0.13935494422912598}
>>> # the following is not so Pythonic >>> numbers = range(10) >>> i = 0 >>> evens = [] >>> while i < len(numbers): >>> if i %2 == 0: evens.append(i) >>> i += 1 >>> [0, 2, 4, 6, 8] >>> # the good way to iterate a range, elegant and efficient >>> evens = [ i for i in range(10) if i%2 == 0] >>> [0, 2, 4, 6, 8]
def _treament(pos, element):
return '%d: %s' % (pos, element)
f = open('test.txt', 'r')
if __name__ == '__main__':
#list comps 1
print sum(len(word) for line in f for word in line.split())
#list comps 2
print [(x + 1, y + 1) for x in range(3) for y in range(4)]
#func
print filter(lambda x: x % 2 == 0, range(10))
#list comps3
print [i for i in range(10) if i % 2 == 0]
#list comps4 pythonic
print [_treament(i, el) for i, el in enumerate(range(10))]
output:
24
[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4)]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
['0: 0', '1: 1', '2: 2', '3: 3', '4: 4', '5: 5', '6: 6', '7: 7', '8: 8', '9: 9']
f = open('/etc/motd, 'r')
longest = max(len(x.strip()) for x in f)
f.close()
return longest
import time
import hashlib
import pickle
from itertools import chain
cache = {}
def is_obsolete(entry, duration):
return time.time() - entry['time'] > duration
def compute_key(function, args, kw):
#序列化/反序列化一个对象,这里是用pickle模块对函数和参数对象进行序列化为一个hash值
key = pickle.dumps((function.func_name, args, kw))
#hashlib是一个提供MD5和sh1的一个库,该结果保存在一个全局字典中
return hashlib.sha1(key).hexdigest()
def memoize(duration=10):
def _memoize(function):
def __memoize(*args, **kw):
key = compute_key(function, args, kw)
# do we have it already
if (key in cache and
not is_obsolete(cache[key], duration)):
print 'we got a winner'
return cache[key]['value']
# computing
result = function(*args, **kw)
# storing the result
cache[key] = {'value': result,-
'time': time.time()}
return result
return __memoize
return _memoize
@memoize()
def very_very_complex_stuff(a, b, c):
return a + b + c
print very_very_complex_stuff(2, 2, 2)
print very_very_complex_stuff(2, 2, 2)
@memoize(1)
def very_very_complex_stuff(a, b):
return a + b
print very_very_complex_stuff(2, 2)
time.sleep(2)
print very_very_complex_stuff(2, 2)
6 we got a winner 6 4 4
switch (var)
{
case v1: func1();
case v2: func2();
...
case vN: funcN();
default: default_func();
}
dictionary实现:
values = {
v1: func1,
v2: func2,
...
vN: funcN,
}
values.get(var, default_func)()
lambda实现:
{
'1': lambda: func1,
'2': lambda: func2,
'3': lambda: func3
}[value]()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有