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

2016 年 8 月 21 日
 thinker3
最多只有语数外三科
u"语 82.5 数 98.5 外 90"
u"语 82.5 外 96"
提取成 dict
3072 次点击
所在节点    Python
6 条回复
billlee
2016 年 8 月 21 日
dict(re.findall(u"(.?)([0-9.]+)", data))
billlee
2016 年 8 月 21 日
一时手抖多了个?, 应该是 (.)([0-9.]+)
tttimit
2016 年 8 月 22 日
r'([语|数|外](1?\d?\d.?5?))'手机打字不易 一个模式就是这样,匹配语文或者数学或者外语加上成绩,成绩可以是带.5 的,来三遍,第二科第三科照我写的来就行,最后用 groups 取值,写好前后加^$。
tttimit
2016 年 8 月 22 日
第一个方括号去掉里面的两条竖线。。
ioiogoo
2016 年 8 月 22 日
如果格式严谨的话
```
string = u"语 82.5 数 98.5 外 90".split(' ')
{string[x]:string[x+1] for x in range(string) if x%2 == 0}
```
sparkssssssss
2016 年 8 月 22 日
新人练手
data = u"语 82.5 数 98.5 外 90"
res = re.findall('(.*?)(\d+(\.\d+)?)',data)
dict = {}
for i in res:
dict[i[0]] = i[1]

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/300825

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX