Upgrade to Pro — share decks privately, control downloads, hide ads and more …

計算名字 list 中開頭字母的人數

計算名字 list 中開頭字母的人數

所謂小題大作就是這個概念?我真的不知道簡介要寫什麼。

如果你真的想知道為什麼 .get 很慢,請看 http://www.slideshare.net/ya790026/why-is-a1-fast-than-aget1

9dafad54b5b4f360b7aae5f482bc1c91?s=128

Tzu-ping Chung

March 26, 2015
Tweet

More Decks by Tzu-ping Chung

Other Decks in Programming

Transcript

  1. None
  2. Python 尝剣 switch

  3. Guido: 欽 dict

  4. counts  =  {}   for  name  in  namelist:    

         if  name[0]  not  in  counts:                  counts[name[0]]  =  1          else:                  counts[name[0]]  +=  1   for  key  in  counts:          count  =  counts[key]
  5. LBYL v. EAFP

  6. Easier to ask for forgiveness than permission.

  7. counts  =  {}   for  name  in  namelist:    

         try:                  counts[name[0]]  +=  1          except  KeyError:                  counts[name[0]]  =  1   for  key  in  counts:          count  =  counts[key]
  8. counts  =  {}   for  name  in  namelist:    

         val  =  counts.get(name[0],  0)          counts[name[0]]  =  val  +  1   for  key  in  counts:          count  =  counts[key]
  9. from  collections  import  defaultdict   counts  =  defaultdict(lambda:  0)  

    for  name  in  namelist:          counts[name[0]]  +=  1   for  key  in  counts:          count  =  counts[key]
  10. from  collections  import  Counter   counts  =  Counter(    

         name[0]  for  name  in  namelist   )   for  key  in  counts:          count  =  counts[key]
  11. from  itertools  import  groupby   for  k,  v  in  groupby(

                     sorted(namelist),                    key=lambda  n:  n[0]):          count  =  len(list(v))
  12. from  itertools  import  groupby   for  k,  v  in  groupby(

                     sorted(namelist),                    key=lambda  n:  n[0]):          count  =  len(list(v)) 馄䌃
  13. Not Really.

  14. counts  =  {}   for  name  in  namelist:    

         if  name[0]  not  in  counts:                  counts[name[0]]  =  1          else:                  counts[name[0]]  +=  1   for  key  in  counts:          count  =  counts[key] Loop
  15. for  k,  v  in  groupby(          

           sorted(namelist),                    key=lambda  n:  n[0]):          count  =  len(list(v)) Loop Implicit loop
  16. Let’s Measure!

  17. • name = str(random.random())[2:] • Exclude import overhead • Benchmarking

    against dict + LBYL
  18. None
  19. ♶䠐㢩

  20. 侶ꆀ䖍㢳Counter 䩝剙㥨欽

  21. ⡎〪銲欽ⵋ EAFP 莃 Ⰹ䒉䊨Ⱗ穠卓鿪䊴♶㢳

  22. EAFP ⮚倳 LBYL

  23. .get 㥨䢧

  24. WHY?

  25. • try is cheap, except is (more) expensive • If

    you hit often, use try-except • If you fail often, use .get
  26. WHY?

  27. Use the source, Luke!

  28. .__getitem__ • Check argument type (string optimisation) • Value lookup

    • Return value on success • Extra checks and KeyError on failure .get • Argument unpacking
 • Value lookup • Return the value on success • Return default value on failure
  29. .__getitem__ • Check argument type (string optimisation) • Value lookup

    • Return value on success • Extra checks and KeyError on failure .get • Argument unpacking
 • Value lookup • Return the value on success • Return default value on failure Super slow! Super fast!
  30. • .get is generally much slower, due to argument unpacking

    • If you fail often, .get’s default value might make up speed
  31. Python function calls are slow.

  32. Fin.