ITPub博客

首页 > Linux操作系统 > Linux操作系统 > A Few Things to Remember While Coding in Python

A Few Things to Remember While Coding in Python

原创 Linux操作系统 作者:jieforest 时间:2012-05-23 12:44:05 0 删除 编辑
[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/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-04-23

  • 博文量
    442
  • 访问量
    521943