#-*-Coding:utf-8-*-
__author__ = ' Administrator '
Import bisect
#排序说明: Http://en.wikipedia.org/wiki/insertion_sort
#简化一些操作
#1: Reduce the workload by cutting out external calls
"""
Note: Another part of program complexity is invoking other functions, methods and classes introduced, in general, should try to put the code outside the loop, for nested loops
This is even more important, instead of repeating calculations in a loop to calculate values before the loop starts, the inner loop should remain concise.
"""
#2: Deque also provides append and Pop methods that can be executed at the same speed at both ends of the sequence
#例子2: The following:
From collections Import Defaultdict,namedtuple
Import Profile,stat
Import Sys
s=[(' A ', 1), (' B ', 2), (' C ', 3)]
@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)
Print slower (), faster ()
Print stat[' dict ']
Print stat[' defaultdict ']
Lg=defaultdict (Long)
Print lg[' one ']
Cus=namedtuple (' curs ', ' firsname ')
C=cus (U ' apped ', U ' Zifoe ')
Print C.firsname
#更多说明
"""
Reducing the complexity of the work can be done by the algorithm can be very good processing efficient data structure to store the implementation of the database, that is, when the solution is not obvious, should consider discard and rewrite the problem part, not for performance
And break the readability of the code!
"""
Python: Learning Defaultdict,namedtuple