Python: Learning Defaultdict,namedtuple

Source: Internet
Author: User

#-*-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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.