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?

1 Answers1

0

Use a regex to parse the wildcards, then iterate:


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)

Output:

81450
81451
81452
81460
81461
81462
81463
81464
81470
81471
81473
81474
81475
81476
81477
Terry Spotts
  • 3,527
  • 1
  • 8
  • 21