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

u"语 82.5 数 98.5 外 90", 正则表达式问题, 没有空格哈,空格是 v2 加的

  •  
  •   thinker3 ·
    thinker3 · 2016-08-21 22:09:19 +08:00 · 2256 次点击
    这是一个创建于 2804 天前的主题,其中的信息可能已经有所发展或是发生改变。
    最多只有语数外三科
    u"语 82.5 数 98.5 外 90"
    u"语 82.5 外 96"
    提取成 dict
    6 条回复    2016-08-22 17:17:40 +08:00
    billlee
        1
    billlee  
       2016-08-21 23:26:25 +08:00
    dict(re.findall(u"(.?)([0-9.]+)", data))
    billlee
        2
    billlee  
       2016-08-21 23:34:43 +08:00
    一时手抖多了个?, 应该是 (.)([0-9.]+)
    tttimit
        3
    tttimit  
       2016-08-22 01:58:43 +08:00 via Android
    r'([语|数|外](1?\d?\d.?5?))'手机打字不易 一个模式就是这样,匹配语文或者数学或者外语加上成绩,成绩可以是带.5 的,来三遍,第二科第三科照我写的来就行,最后用 groups 取值,写好前后加^$。
    tttimit
        4
    tttimit  
       2016-08-22 01:59:31 +08:00 via Android
    第一个方括号去掉里面的两条竖线。。
    ioiogoo
        5
    ioiogoo  
       2016-08-22 09:07:31 +08:00
    如果格式严谨的话
    ```
    string = u"语 82.5 数 98.5 外 90".split(' ')
    {string[x]:string[x+1] for x in range(string) if x%2 == 0}
    ```
    coolloves
        6
    coolloves  
       2016-08-22 17:17:40 +08:00
    新人练手
    data = u"语 82.5 数 98.5 外 90"
    res = re.findall('(.*?)(\d+(\.\d+)?)',data)
    dict = {}
    for i in res:
    dict[i[0]] = i[1]
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3106 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 14:59 · PVG 22:59 · LAX 07:59 · JFK 10:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.