0

I have a large dataset with rows of wildcarded strings e.g.

8145[012]
8146[01234]
8147[0134567]

I would like an efficient way to expand these out into unique rows for each combination, e.g.

81450
81451
81452
81460
81461
81462
etc...

What is the most efficient way to do this in Python 3.7?

4

1 回答 1

0

使用正则表达式解析通配符,然后迭代:


import re

data = ['8145[012]', '8146[01234]', '8147[0134567]']

for wildcard in data:
    base, combos = re.search(r'(\d+)\[(\d+)\]', wildcard).groups()
    for combo in combos:
        print(base + combo)

输出:

81450
81451
81452
81460
81461
81462
81463
81464
81470
81471
81473
81474
81475
81476
81477
于 2020-11-13T22:21:22.467 回答