方法一:

 import operator

 def average(*args):
return reduce(operator.add, args) / len(args) if args else 0

注释:

  • 语句 if []: / if (): / if '' : / if {}: 对于以上空集 逻辑值为 False
  • 等同于 if len(list) == 0 : pass 等

方法二:

 import functools

 def average(*args):
acc = functools.partial(reduce, lambda x, y : x + y)
# acc = functools.partial(reduce, operator.add)
return acc(args) / len(args) if args else 0

注释:

  • acc 为初值为lambda 表达式的reduce偏函数

Idiomatic Python手记一: average in FP way的更多相关文章

  1. 《Writing Idiomatic Python》前两部分的中文翻译

    汇总了一下这本小书前两部分的内容: 翻译<Writing Idiomatic Python>(一):if语句.for循环 翻译<Writing Idiomatic Python> ...

  2. 翻译《Writing Idiomatic Python》(五):类、上下文管理器、生成器

    原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...

  3. 翻译《Writing Idiomatic Python》(四):字典、集合、元组

    原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...

  4. 翻译《Writing Idiomatic Python》(三):变量、字符串、列表

    原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...

  5. 翻译《Writing Idiomatic Python》(一):if语句、for循环

    开篇废话 这是在美国Amazon上评价很不错的一本书,其实严格来说这可能不算书,而是一本小册子.就像书名一样,里面的内容主要是用一些例子讲述地道的Python的代码是怎样写的.书中把很多例子用不良风格 ...

  6. 翻译《Writing Idiomatic Python》(二):函数、异常

    原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...

  7. 分享书籍[writing idiomatic python ebook]

    你是不是总是觉得学了python好久,蓦然回首,总是感觉写的代码不是那么有pythonic的味道.看看别人的代码(django,webpy),再看看自己的代码,觉得就是一java-python的混合体 ...

  8. Python——Code Like a Pythonista: Idiomatic Python

    Code Like a Pythonista: Idiomatic Python 如果你有C++基础,那学习另一门语言会相对容易.因为C++即面向过程,又面向对象.它很底层,能像C一样访问机器:它也很 ...

  9. 分享书籍[writing idiomatic python ebook] 二

    对多个变量设置相同的值时,用连等号一起赋值 x = 10 y = 10 z = 10 改成: x = y = z = 10 交换变量值时,可以避免定义新的临时变量 x = 10 y = 5 temp ...

随机推荐

  1. 1057: [ZJOI2007]棋盘制作 - BZOJ

    Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴 ...

  2. kill 进程卡住,超时kill方法

    还是有漏洞 ,万一 working.py未超时, kill_job.sh 会不会杀死别人的进程啊start.sh#!/bin/bash python working.py &python wo ...

  3. 【高斯消元】Poj 1222:EXTENDED LIGHTS OUT

    Description In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each ...

  4. uva 701

    参考了一下http://hi.baidu.com/renxl51/item/e80b688f9f54aadd5e0ec1de 给一个数字x,求最小的正整数e,使得pow(2,e) == x*pow(1 ...

  5. [itint5]树中最大路径和

    http://www.itint5.com/oj/#13 要注意,一是空路径也可以,所以最小是0.然后要时刻注意路径顶多有两条子路径+根节点组成,所以更新全局最值时和返回上一级的值要注意分清. #in ...

  6. ajax readyState的五种状态详解

    通过ajax的readyState的值,我们可以知道当前的这个http请求处于什么状态.对于web的调试是比较重要的. readyState 状态说明: (0)未初始化 此阶段确认XMLHttpReq ...

  7. Android tabhost下的activity怎样获取传来的值

    android tabhost下的activity怎样获取传来的值,具体解决方案如下: 解决方案: 其他activity设置intent:Intent intent=new Intent(); int ...

  8. PostgreSQL和MySQL like区别

    前言:今天在PostgreSQL中使用like,字段类型是int,执行语句报错, 1.表结构:都是用sysbench工具产生的 postgres=# \d sbtest1;               ...

  9. mysql 更新唯一主键列 被堵塞

    mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEAT ...

  10. C#中的Marshal

    Const.MaxLengthOfBufferd的长度固定为0x2000   也就是8192 private bool SendMessage(int messageType, string ip, ...