V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 5689 views
    This topic created in 2961 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
        1
    F281M6Dh8DXpD1g2  
       Mar 19, 2018
    打表呗
    rrfeng
        2
    rrfeng  
       Mar 19, 2018 via Android
    写个 dict 存起来。
    lhx2008
        3
    lhx2008  
       Mar 19, 2018 via Android
    建个 map,从 map 里面取值
    IanPeverell
        4
    IanPeverell  
       Mar 19, 2018
    那就用 switch 啊
    Zzde
        5
    Zzde  
       Mar 19, 2018 via iPhone
    封装成方法 用 getattr 调
    araraloren
        6
    araraloren  
       Mar 19, 2018
    @IanPeverell python not have switch
    lfzyx
        7
    lfzyx  
       Mar 19, 2018
    你把正确的都放在一个 list 里面,['Sun Ltd', 'Running well Ltd' , 'nice fitting Ltd ']

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

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

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


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

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