推荐学习书目
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 · Aug 20, 2016 · 1997 views
    This topic created in 3623 days ago, the information mentioned may be changed or developed.
    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 replies    2016-08-20 21:20:25 +08:00
    7sDream
        1
    7sDream  
       Aug 20, 2016
    print([f() for f in count()])

    另外发帖之前最好看一下右边发帖提示里的 markdown 语法,代码发出来要有语法高亮和缩进这样比较方便看。
    prefere
        2
    prefere  
       Aug 20, 2016   ❤️ 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
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5470 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 08:43 · PVG 16:43 · LAX 01:43 · JFK 04:43
    ♥ Do have faith in what you're doing.