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
Newyorkcity
V2EX  ›  Python

自定义函数把内部函数加到数组内了,应该怎么直接出指?

  •  
  •   Newyorkcity · 2016-08-20 20:34:48 +08:00 · 1531 次点击
    这是一个创建于 2815 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def count():
    fs = []
    for i in range(1, 4):
    def f():
    return i*i
    fs.append(f)
    return fs
    以上是函数代码
    print(count()())表示 list object is not callable....然后想起 count()返回的其实是个[],就试了一下
    print(count[1]()),出了结果 9...
    可如果我想获得的结果是
    [9,9,9]的话该怎么输入呢?可以实现吗?
    谢谢
    2 条回复    2016-08-20 21:20:25 +08:00
    7sDream
        1
    7sDream  
       2016-08-20 21:17:33 +08:00
    print([f() for f in count()])

    另外发帖之前最好看一下右边发帖提示里的 markdown 语法,代码发出来要有语法高亮和缩进这样比较方便看。
    prefere
        2
    prefere  
       2016-08-20 21:20:25 +08:00   ❤️ 1
    python2.7
    def count():
    fs = []
    for i in range(1, 4):
    def f():
    return i*i
    fs.append(f)
    return fs
    a = []
    for i in count():
    a.append(i())
    print a
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   831 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:28 · PVG 03:28 · LAX 12:28 · JFK 15:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.