A Few Things to Remember While Coding in Python

jieforest發表於2012-05-23
[i=s] 本帖最後由 jieforest 於 2012-5-21 10:24 編輯

Zen of Python


Learning theculture that surrounds a language brings you one step closer to being a betterprogrammer. If you haven’t read the Zen of Python yet open a Python prompt andtype import this. For each of the item on the list you canfind examples herehttp://artifex.org/~hblanks/talks/2011/pep20_by_example.html


One caught my attention:


Beautifulis better than ugly


Give me a function that takes a list ofnumbers and returns only the even ones, divided by two.

CODE:

  #-----------------------------------------------------------------------
  
  halve_evens_only = lambda nums: map(lambda i: i/2, filter(lambda i: not i%2, nums))
  
  #-----------------------------------------------------------------------
  
  def halve_evens_only(nums):
      return [i/2 for i in nums if not i % 2]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-730670/,如需轉載,請註明出處,否則將追究法律責任。

相關文章