如何在python中用逗号分隔数字组?

时间:2018-04-16 15:45:07

标签: python regex

我有以下文字:

Cluster 7: {4, 15, 21, 28, 33, 35, 43, 47, 53, 57, 59, 66,
       69, 70, 74, 86, 87, 88, 90, 114, 136, 148, 201,
       202, 212, 220, 227, 250, 252, 253, 259, 262, 267,
       270, 282, 296, 318, 319, 323, 326, 341}
Cluster 8: {9, 10, 11, 20, 39, 55, 79, 101, 108, 143, 149,
       221, 279, 284, 285, 286, 287, 327, 333, 334, 335,
       336}
Cluster 9: {3, 64, 83, 93, 150, 153, 264, 269, 320, 321, 322}
Cluster 10: {94, 123, 147}

我想通过群集提取每组中的数字。

我尝试使用正则表达式没有太多运气

我试过了:

regex="(Cluster \d+): \{((\d+)[,\}][\n ]+)+|(?:(\d+),[\n ])"

但这些团体不匹配。

我希望输出为:

["Cluster 7", '4', '15', '21', '28', '33', '35', '43', '47', '53', '57', '59', '66', '69', '70', '74', '86', '87', '88', '90', '114', '136', '148', '201', '202', '212', '220', '227', '250', '252', '253', '259', '262', '267', '270', '282', '296', '318', '319', '323', '326', '341', "Cluster 8", '9', '10', '11', '20', '39', '55', '79', '101', '108', '143', '149', '221', '279', '284', '285', '286', '287', '327', '333', '334', '335', '336', "Cluster 9", '3', '64', '83', '93', '150', '153', '264', '269', '320', '321', '322', "Cluster 10", "94", "123", "147"]

或许这可能不是最佳方法。

由于

3 个答案:

答案 0 :(得分:4)

我不会使用正则表达式。您的文字符合yaml规范,可以直接使用an order-preserving yaml loader such as oyaml加载。

import oyaml as yaml   # pip install oyaml
data = yaml.load(text)

要将该dict解压缩到所需的“平面”结构,这只是一个列表理解:

[x for (k, v) in data.items() for x in (k, *v)]

注意:我是oyaml的作者。

答案 1 :(得分:3)

您可以创建更通用的正则表达式:

> (test2)
in-producer: stop condition for multiple values must be a predicate, got: #<void>

输出:

import re
s = '\nCluster 7: {4, 15, 21, 28, 33, 35, 43, 47, 53, 57, 59, 66,\n       69, 70, 74, 86, 87, 88, 90, 114, 136, 148, 201,\n       202, 212, 220, 227, 250, 252, 253, 259, 262, 267,\n       270, 282, 296, 318, 319, 323, 326, 341}\nCluster 8: {9, 10, 11, 20, 39, 55, 79, 101, 108, 143, 149,\n       221, 279, 284, 285, 286, 287, 327, 333, 334, 335,\n       336}\nCluster 9: {3, 64, 83, 93, 150, 153, 264, 269, 320, 321, 322}\nCluster 10: {94, 123, 147}\n'
data = re.findall('Cluster \d+|\d+', s)

答案 2 :(得分:1)

See regex in use here

\w+(?: +\w+)?
  • \w+匹配一个或多个单词字符
  • (?: +\w+)?可选择匹配以下内容
    • +匹配一个或多个空格
    • \w+匹配一个或多个单词字符

See code in use here

import re

s = "Cluster 7: {4, 15, 21, 28, 33, 35, 43, 47, 53, 57, 59, 66,\n       69, 70, 74, 86, 87, 88, 90, 114, 136, 148, 201,\n       202, 212, 220, 227, 250, 252, 253, 259, 262, 267,\n       270, 282, 296, 318, 319, 323, 326, 341}\nCluster 8: {9, 10, 11, 20, 39, 55, 79, 101, 108, 143, 149,\n       221, 279, 284, 285, 286, 287, 327, 333, 334, 335,\n       336}\nCluster 9: {3, 64, \n3, 93, 150, 153, 264, 269, 320, 321, 322}\nCluster 10: {94, 123, 147}"
print(re.findall(r"\w+(?: +\w+)?", s))

结果:

['Cluster 7', '4', '15', '21', '28', '33', '35', '43', '47', '53', '57', '59', '66', '69', '70', '74', '86', '87', '88', '90', '114', '136', '148', '201', '202', '212', '220', '227', '250', '252', '253', '259', '262', '267', '270', '282', '296', '318', '319', '323', '326', '341', 'Cluster 8', '9', '10', '11', '20', '39', '55', '79', '101', '108', '143', '149', '221', '279', '284', '285', '286', '287', '327', '333', '334', '335', '336', 'Cluster 9', '3', '64', '83', '93', '150', '153', '264', '269', '320', '321', '322', 'Cluster 10', '94', '123', '147']