V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Kung815
V2EX  ›  Python

为什么这个接受一个 list 并求积的函数返回 none?

  •  
  •   Kung815 · 2017-08-01 10:57:48 +08:00 · 2285 次点击
    这是一个创建于 2450 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如图

    8 条回复    2017-08-09 21:24:33 +08:00
    lichun
        1
    lichun  
       2017-08-01 11:14:24 +08:00
    return 呢?
    fzxml
        2
    fzxml  
       2017-08-01 11:30:43 +08:00
    看标题我就猜没 return 23333 毕竟我也碰到过
    ryd994
        3
    ryd994  
       2017-08-01 16:30:21 +08:00 via Android
    prod = lambda L: reduce (lambda x,y : x*y , L)
    唔………
    感觉又是一行无法维护的 lambda 地狱………
    ryd994
        4
    ryd994  
       2017-08-01 16:33:28 +08:00 via Android
    def prod(L):
    return reduce(lambda x,y : x*y, L)

    这样好点
    Kung815
        5
    Kung815  
    OP
       2017-08-02 16:20:42 +08:00
    @fzxml 不太明白, return 我不是已经加了嘛
    Kung815
        6
    Kung815  
    OP
       2017-08-02 16:21:10 +08:00
    @lichun 已经加了 return 了
    fzxml
        7
    fzxml  
       2017-08-03 16:37:18 +08:00
    @Kung815
    ```
    from functools import reduce

    def prod(L):
    def qiuji(x, y):
    return x * y
    return reduce(qiuji, L)
    print(prod([3, 5, 7, 8]))
    ```
    symons
        8
    symons  
       2017-08-09 21:24:33 +08:00
    你这个没有 return 啊,prod 函数得有一个返回值
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1008 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:04 · PVG 04:04 · LAX 13:04 · JFK 16:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.