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

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

  •  
  •   youthfire · 2018-03-19 16:01:11 +08:00 · 4983 次点击
    这是一个创建于 2202 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    目前是这种写法

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

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

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

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

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

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

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


    这和 python 也没太大关系, 你需要的是一个 dict(map)
    beforeuwait
        13
    beforeuwait  
       2018-03-19 17:09:08 +08:00
    一楼说得对
    youthfire
        14
    youthfire  
    OP
       2018-03-19 17:22:04 +08:00
    感谢各位!

    之前只知道 list 和 dic,但并不清楚 dict(map),表驱动这回事。只觉得尽管可以用来判断是否在一个 pool 内,但缺乏映射关系的表达。可能也是没有系统的学习有关。已经达成。谢谢!
    IanPeverell
        15
    IanPeverell  
       2018-03-19 17:33:14 +08:00
    @araraloren 最近 node 写多了,忘了 python 没有了(捂脸跑)
    okzpy9425
        16
    okzpy9425  
       2018-03-19 22:07:01 +08:00
    hashmap
    ivydom
        17
    ivydom  
       2018-03-19 22:13:52 +08:00
    用字典
    liuyin
        18
    liuyin  
       2018-03-20 09:23:11 +08:00
    dict 这个主意不错
    Ge4Los
        19
    Ge4Los  
       2018-03-20 18:25:45 +08:00
    表驱动了解下。
    咦,有人讲过了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4468 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 10:02 · PVG 18:02 · LAX 03:02 · JFK 06:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.