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

Python 大量的 if statements,有没有漂亮简洁的写法?

  •  
  •   youthfire · Mar 19, 2018 · 6140 views
    This topic created in 3157 days ago, the information mentioned may be changed or developed.

    主要用途是对渐入的关键字进行“修正”,以完全匹配目标表内的查询字符。

    目前是这种写法

    if keystring == 'sun': keystring = 'Sun Ltd.,' if keystring == 'run': keystring = 'Running well Ltd.,' if keystring == 'fit': keystring = 'nice fitting Ltd.,'

    这样简单的替换可能有几十个,长长一串特别难看。最终替换成的目标又不是一样的,也没法用函数。

    求教有没有什么好看一些的写法?不用写个几十行?

    19 replies  •  2018-03-20 18:25:45 +08:00
    F281M6Dh8DXpD1g2
    F281M6Dh8DXpD1g2
    Mar 19, 2018
    1
    打表呗
    rrfeng
    rrfeng
    Mar 19, 2018 via Android
    2
    写个 dict 存起来。
    lhx2008
    lhx2008
    Mar 19, 2018 via Android
    3
    建个 map,从 map 里面取值
    IanPeverell
    IanPeverell
    Mar 19, 2018
    4
    那就用 switch 啊
    Zzde
    Zzde
    Mar 19, 2018 via iPhone
    5
    封装成方法 用 getattr 调
    araraloren
    araraloren
    Mar 19, 2018
    6
    @IanPeverell python not have switch
    lfzyx
    lfzyx
    Mar 19, 2018
    7
    你把正确的都放在一个 list 里面,['Sun Ltd', 'Running well Ltd' , 'nice fitting Ltd ']

    然后把用户输入的去匹配列表中的,取出来,不就行了
    wbgbg
    wbgbg
    Mar 19, 2018
    8
    表驱动了解一下
    snailsir
    snailsir
    Mar 19, 2018
    9
    代码大全,表驱动法
    wodexiaogou
    wodexiaogou
    Mar 19, 2018
    10
    写个字典{'sun':'Sun Ltd'}这种,for i in dict:keystring=dict[i]
    jyf
    jyf
    Mar 19, 2018
    11
    表驱动

    ```python
    replaces = {'sun': 'sun ltd', 'oracle': 'oracle ltd', }
    for k in keys:
    v = replaces.get(k, '')
    ```

    不过看你这个需求有点像关键词屏蔽 :D 这种其实你用正则把许多关键词用|连起来也可以的
    ToughGuy
    ToughGuy
    Mar 19, 2018❤️1
    12
    corps = {'sun': 'Sun Ltd.,'}
    keystring = corps.get(keystring, default='Other')


    这和 python 也没太大关系, 你需要的是一个 dict(map)
    beforeuwait
    beforeuwait
    Mar 19, 2018
    13
    一楼说得对
    youthfire
    youthfire
    OP
    Mar 19, 2018
    14
    感谢各位!

    之前只知道 list 和 dic,但并不清楚 dict(map),表驱动这回事。只觉得尽管可以用来判断是否在一个 pool 内,但缺乏映射关系的表达。可能也是没有系统的学习有关。已经达成。谢谢!
    IanPeverell
    IanPeverell
    Mar 19, 2018
    15
    @araraloren 最近 node 写多了,忘了 python 没有了(捂脸跑)
    okzpy9425
    okzpy9425
    Mar 19, 2018
    16
    hashmap
    ivydom
    ivydom
    Mar 19, 2018
    17
    用字典
    l4y3
    l4y3
    Mar 20, 2018
    18
    dict 这个主意不错
    Ge4Los
    Ge4Los
    Mar 20, 2018
    19
    表驱动了解下。
    咦,有人讲过了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1033 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 22:47 · PVG 06:47 · LAX 14:47 · JFK 17:47
    ♥ Do have faith in what you're doing.