【发布时间】:2017-11-29 09:28:46
【问题描述】:
例子:
import regex
import itertools
m = "90.80.19 90.43.19 908019 92.11.15 90.80.19 930000"
reg = regex.compile("\d\d\.?\d\d\.?\d\d")
[list(g) for k, g in itertools.groupby(sorted(reg.findall(m)))]
Output: [['90.43.19'], ['90.80.19', '90.80.19'], ['908019'], ['92.11.15'], ['930000']]
groupby() 组合双精度:只有双精度 90.80.19 已分组。
我想要做的是按上面的正则表达式分组:\.? 在上面的正则表达式中是可选的。
Expected output: [['90.43.19'], ['90.80.19', '90.80.19', '908019'], ['92.11.15'], ['930000']]
是否可以让 groupby() 有条件的分组?
【问题讨论】:
标签: python regex python-3.x pattern-matching itertools