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

Python 中 list() 与 [] 有什么区别吗

  •  
  •   limyel · 2020-03-22 17:23:17 +08:00 · 3820 次点击
    这是一个创建于 1488 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在模仿 bottle 写一个迷你 web 框架,有一个地方是在构造响应头

    	start_response(status, list(response.header.items()))
    

    其中 response.header.items 是一个生成器,如上述代码中用 list(),生成器就会自动展开,但是用 [] 就只是返回一个包含生成器的列表。

    所以请假一下大家,这两者是有什么区别吗

    14 条回复    2020-03-24 10:08:19 +08:00
    ahdw
        1
    ahdw  
       2020-03-22 17:34:32 +08:00
    据我的经验,list() 函数是把括号里的东西转换成 list 格式,也可以用来创建 list []
    Trim21
        2
    Trim21  
       2020-03-22 17:36:07 +08:00 via iPhone
    括号就是正常的 list 语法,相当于写了一个[generator,]
    你要是想展开可以用[x for x in generator]
    ahdw
        3
    ahdw  
       2020-03-22 17:36:46 +08:00
    [ ] 是创建一个 list,内容是方括号里面的。

    ( cmd + enter 竟然直接把我写了一半的东西发表了……)
    nnqijiu
        4
    nnqijiu  
       2020-03-22 17:38:32 +08:00
    list()可以把别的对象转成 list 对象
    sagaxu
        5
    sagaxu  
       2020-03-22 17:41:43 +08:00 via Android   ❤️ 4
    写框架前先把基本语法看一遍吧...
    imn1
        6
    imn1  
       2020-03-22 17:58:38 +08:00
    list(obj) --> list_object[:] ≈= obj[:]
    [obj] --> list_object[0] == obj

    list("abc") --> ['a', 'b', 'c']
    ["abc"] --> ['abc', ]
    inhzus
        7
    inhzus  
       2020-03-22 18:01:18 +08:00 via Android
    没记错的话 [] 作为函数参数的缺省值会发生出乎意料的事情
    hxse
        8
    hxse  
       2020-03-22 18:30:16 +08:00
    >>> a=(i for i in range(5))
    >>> b=(i for i in range(5))
    >>> list(a)==[*b]
    True
    lithbitren
        9
    lithbitren  
       2020-03-23 00:39:37 +08:00
    方括号里面没加星号吧,加星号没见过出啥问题,除非直接上列表解析式才不用加星号,list(iterator)和[*iterator]没遇到过啥不等价问题,后者还快些,但 dict(kwargs)和{**kwargs}就不太一样,前者可以解析子元素长度为 2 的可迭代对象,后者不行。
    lithbitren
        10
    lithbitren  
       2020-03-23 00:42:52 +08:00
    start_response(status, list(response.header.items())) 写成 start_response(status, [*response.header.items()])估计就没啥问题了。
    limyel
        11
    limyel  
    OP
       2020-03-23 11:38:00 +08:00
    @ahdw
    @Trim21
    @nnqijiu
    感谢解答🙏
    limyel
        12
    limyel  
    OP
       2020-03-23 11:38:18 +08:00
    @sagaxu
    哈哈哈新人想不自量力一下
    limyel
        13
    limyel  
    OP
       2020-03-23 11:39:03 +08:00
    @lithbitren
    我明白了,感谢🙏
    bnm965321
        14
    bnm965321  
       2020-03-24 10:08:19 +08:00
    [] 这是 literal 构造器,字面量构造器
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3173 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:34 · PVG 08:34 · LAX 17:34 · JFK 20:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.