推荐学习书目
› 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
loliordie
V2EX  ›  Python

这种写法是什么奇技淫巧?

  •  
  •   loliordie · Apr 10, 2019 · 7782 views
    This topic created in 2768 days ago, the information mentioned may be changed or developed.
    list1 = [(v[0],v[1])
    for v in box]
    居然还能工作...
    25 replies  •  2019-04-12 11:12:54 +08:00
    Dvel
    Dvel
    Apr 10, 2019❤️6
    1
    这不就是列表生成式吗,一般不换行吧
    loliordie
    loliordie
    OP
    Apr 10, 2019
    2
    @Dvel 卧槽眼花了,一换行就不认识了。。。想了半天为什么调用前就工作了。。。
    lilpig
    lilpig
    Apr 10, 2019 via Android
    3
    我很好奇为啥要换行写。。我也是缓了一下才看出来😂
    momuou
    momuou
    Apr 10, 2019
    4
    三思而后行。。别发出来闹出笑话。。
    imycc
    imycc
    Apr 10, 2019
    5
    讲真的我还真看到有人这么写,因为 tuple 那部分太长了。
    不过像你这种情况我更喜欢写 map
    www5070504
    www5070504
    Apr 10, 2019
    6
    解析每行的那种格式化数据 比如 xx=yy 之类的 这样的写法好像挺常见的
    wxVIP
    wxVIP
    Apr 10, 2019
    7
    @momuou 题主小伙子应该是没见过这种格式的
    shawndev
    shawndev
    Apr 10, 2019
    8
    列表生成式,将前两项转为 tuple
    kiddult
    kiddult
    Apr 10, 2019
    9
    @wuxi889 应该是被换行整懵了。。。。我第一眼也是
    oblivious
    oblivious
    Apr 10, 2019
    10
    box = [[1, 2, 3, 4, 5],
    [2, 3, 4, 5, 6]]

    list1 = [(v[0],v[1]) for v in box]

    => list1 = [(1, 2), (2, 3)]
    lithiumii
    lithiumii
    Apr 10, 2019❤️1
    11
    [] 里面放简单的 for 循环生成新的 list 是标准的 list comprehension 操作
    利用括号(大中小括号都行)内直接换行有 implicit line joining,也是换行的标准操作

    不过换行一般是为了可读性考虑,也许作者觉得这样换行更清晰吧
    Orenoid
    Orenoid
    Apr 10, 2019❤️1
    12
    还可以这么写
    [(foo, bar) for foo, bar, *_ in list1]
    xuwangyuan520
    xuwangyuan520
    Apr 10, 2019 via Android
    13
    我是初学者,我感觉挺正常的,方便以后挖沟!
    CallMeReznov
    CallMeReznov
    Apr 10, 2019
    14
    今天早上看 NVM 安装后要执行的 export 命令
    发现他赋值后换行一个[]起来的,我怎么都不明白这个[]是什么意思,难道是上一行的?但是为什么没有\?
    后来一查原来是 if then 简写...真是无时无刻不感觉自己菜..
    kidlj
    kidlj
    Apr 10, 2019
    15
    "There should be one-- and preferably only one --obvious way to do it."
    BBCCBB
    BBCCBB
    Apr 10, 2019
    16
    第一眼我也看懵
    tiedan
    tiedan
    Apr 10, 2019
    17
    换行为了可读性
    retanoj
    retanoj
    Apr 10, 2019
    18
    行了,这下问题变成“为啥换行还能工作”了
    phizumi
    phizumi
    Apr 10, 2019 via iPhone
    19
    @Orenoid 这种写法可读性更好,👍🏻
    wedoub
    wedoub
    Apr 10, 2019
    20
    Pythonic 的写法,看来你还不够 Pythonic
    shintendo
    shintendo
    Apr 10, 2019
    21
    想起 C++那个-->操作符的段子
    megachweng
    megachweng
    Apr 10, 2019 via iPhone
    22
    如果 v 是个 tuple 可以 for a,b in box 不用 v[0] v[1]
    robinlovemaggie
    robinlovemaggie
    Apr 10, 2019
    23
    还可以更屌的这样写:
    generList1 = ((v[0],v[1]) for v in box)
    halk
    halk
    Apr 11, 2019
    24
    @tiedan 换行影响力了我的阅读,看来果然不够 Pythonic
    moxiaowei
    moxiaowei
    Apr 12, 2019
    25
    这特么不是 python 的基础么
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1642 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 83ms · UTC 10:41 · PVG 18:41 · LAX 02:41 · JFK 05:41
    ♥ Do have faith in what you're doing.