Python 中如何优雅地写多行列表解析? - V2EX
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
lightening
V2EX    Python

Python 中如何优雅地写多行列表解析?

  •  
  •   lightening
    SteveLTN 2016-11-29 03:33:57 +08:00 6461 次点击
    这是一个创建于 3286 天前的主题,其中的信息可能已经有所发展或是发生改变。

    平时写 Ruby 比较多,最近要写些 Python 。比如我有一个表格,需要对每个单元格做一些操作,然后返回一个新表格,用 Ruby ,我会这样:

    # Ruby table.map { |row| row.map { |cell| ... do_something(cell) ... } } 

    如果这个 do_something 逻辑需要多行,如何在 Python 中优雅的实现呢?

    试过:

    列表解析:

    [[do_something(cell) for cell in row] for row in table] 

    但是我不知道如何优雅地扩展成多行。

    同样的,用 map 也难以扩展成多行:

    list(map(lambda row: list(map(lambda cell: do_something(cell), row)), table)) 

    当然我可以把要做的操作定义成方法,不过方法多了也比较麻烦。

    请问最优雅的做法是什么?

    10 条回复    2016-11-29 15:35:09 +08:00
    binux
        1
    binux  
       2016-11-29 03:47:27 +08:00
    for row in table:
    for cell in row:
    do_something(cell)
    lightening
        2
    lightening  
    OP
       2016-11-29 03:50:53 +08:00
    @binux 我要不改动原 table 的情况下新建一个 table ,这样的话就要不停地手动建立元素然后插入新列表
    binux
        3
    binux  
       2016-11-29 03:56:09 +08:00   1
    @lightening 那你就在 do_something 里面写多几行不就行了
    czheo
        4
    czheo  
       2016-11-29 05:39:16 +08:00   1
    Python 顶多就这样了。 lambda 只能单行,不比 ruby 的 block 灵活。

    def do_something(cell):
    ____pass

    [[do_something(cell) for cell in row] for row in table]
    wellsc
        5
    wellsc  
       2016-11-29 09:27:54 +08:00 via Android
    用 map
    zmrenwu
        6
    zmrenwu  
       2016-11-29 09:41:43 +08:00   1
    import pandas as pd
    wwulfric
        7
    wwulfric  
       2016-11-29 10:43:06 +08:00   1
    同 4L , Python 的 lambda 比较费,只能提前建好一个函数了
    hanbaobao2005
        8
    hanbaobao2005  
       2016-11-29 11:10:26 +08:00   1
    不建议使用:
    [[do_something(cell) for cell in row] for row in table]
    这种写法,有坑。
    hanbaobao2005
        9
    hanbaobao2005  
       2016-11-29 11:19:45 +08:00
    哦。我也想把具体的 “坑” 说清楚,但时间太久了。 Orz...
    woostundy
        10
    woostundy  
       2016-11-29 15:35:09 +08:00   1
    map(lambda x:map(do_something,x),a)
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5192 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 06:00 PVG 14:00 LAX 22:00 JFK 01:00
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86