aerin
aerin

Reputation: 22694

python substitute substring with multiple values with all possible combinations

The thing that I'm trying to achieve:

  {
    "templateQuery": "<SkvQNzAdK0i4UEEe19U3Gw> does <6lLJMyzKU2bCmJzZxZnA> <7ERH9WT1AEi2ST9Y3BvMOw> their time?"
  }

I'd like to substitute every key <id> with all possible combinations of values <aliases>. Therefore, it can consitute a sentence like How does Peter manage their time?

For example,

  {
    "name": "How_QE",
    "aliases": [
      "how", "How"
    ],
    "id": "SkvQNzAdK0i4UEEe19U3Gw",
  },

  {
    "name": "PersonName",
    "aliases": ["Peter", "Julie", "Judy", "Mac"],
    "id": "6lLJMyzKU2bCmJzZxZnA",
    "schemaType": "Entity"
  },

  {
    "name": "Spend",
    "aliases": [
      "organize",
      "allocate",
      "manage",
      "spent",
      "spend",
      "organize"
    ],
    "id": "7ERH9WT1AEi2ST9Y3BvMOw",
    "schemaType": "Entity"
  },

What's the best way to achieve this in python? I thought of using Template (https://docs.python.org/dev/library/string.html#template-strings) however, it can only handle a single candidate. For me, I want to substitute all possible combinations, which is in this example 2 * 4 * 6 = 48 variations.

Upvotes: 1

Views: 75

Answers (1)

Ajax1234
Ajax1234

Reputation: 71461

You can use itertools.product:

import itertools, re
def find_alias(_d:list, _id:str) -> str:
  return [c['aliases'] for c in _d if c['id'] == _id][0] 

t = {"templateQuery": "<SkvQNzAdK0i4UEEe19U3Gw> does <6lLJMyzKU2bCmJzZxZnA> <7ERH9WT1AEi2ST9Y3BvMOw> their time?"}
d = [{'name': 'How_QE', 'aliases': ['how', 'How'], 'id': 'SkvQNzAdK0i4UEEe19U3Gw'}, {'name': 'PersonName', 'aliases': ['Peter', 'Julie', 'Judy', 'Mac'], 'id': '6lLJMyzKU2bCmJzZxZnA', 'schemaType': 'Entity'}, {'name': 'Spend', 'aliases': ['organize', 'allocate', 'manage', 'spent', 'spend', 'organize'], 'id': '7ERH9WT1AEi2ST9Y3BvMOw', 'schemaType': 'Entity'}]
all_aliases = [find_alias(d, i) for i in re.findall('(?<=\<).*?(?=\>)', t['templateQuery'])]
new_t = [(lambda c:{'templateQuery':re.sub('(?<=\<).*?(?=\>)', lambda x:next(c), t['templateQuery'])})(iter(i)) \
   for i in itertools.product(*all_aliases)]

print(len(new_t))

Output:

[{'templateQuery': '<how> does <Peter> <organize> their time?'}, {'templateQuery': '<how> does <Peter> <allocate> their time?'}, {'templateQuery': '<how> does <Peter> <manage> their time?'}, {'templateQuery': '<how> does <Peter> <spent> their time?'}, {'templateQuery': '<how> does <Peter> <spend> their time?'}, {'templateQuery': '<how> does <Peter> <organize> their time?'}, {'templateQuery': '<how> does <Julie> <organize> their time?'}, {'templateQuery': '<how> does <Julie> <allocate> their time?'}, {'templateQuery': '<how> does <Julie> <manage> their time?'}, {'templateQuery': '<how> does <Julie> <spent> their time?'}, {'templateQuery': '<how> does <Julie> <spend> their time?'}, {'templateQuery': '<how> does <Julie> <organize> their time?'}, {'templateQuery': '<how> does <Judy> <organize> their time?'}, {'templateQuery': '<how> does <Judy> <allocate> their time?'}, {'templateQuery': '<how> does <Judy> <manage> their time?'}, {'templateQuery': '<how> does <Judy> <spent> their time?'}, {'templateQuery': '<how> does <Judy> <spend> their time?'}, {'templateQuery': '<how> does <Judy> <organize> their time?'}, {'templateQuery': '<how> does <Mac> <organize> their time?'}, {'templateQuery': '<how> does <Mac> <allocate> their time?'}, {'templateQuery': '<how> does <Mac> <manage> their time?'}, {'templateQuery': '<how> does <Mac> <spent> their time?'}, {'templateQuery': '<how> does <Mac> <spend> their time?'}, {'templateQuery': '<how> does <Mac> <organize> their time?'}, {'templateQuery': '<How> does <Peter> <organize> their time?'}, {'templateQuery': '<How> does <Peter> <allocate> their time?'}, {'templateQuery': '<How> does <Peter> <manage> their time?'}, {'templateQuery': '<How> does <Peter> <spent> their time?'}, {'templateQuery': '<How> does <Peter> <spend> their time?'}, {'templateQuery': '<How> does <Peter> <organize> their time?'}, {'templateQuery': '<How> does <Julie> <organize> their time?'}, {'templateQuery': '<How> does <Julie> <allocate> their time?'}, {'templateQuery': '<How> does <Julie> <manage> their time?'}, {'templateQuery': '<How> does <Julie> <spent> their time?'}, {'templateQuery': '<How> does <Julie> <spend> their time?'}, {'templateQuery': '<How> does <Julie> <organize> their time?'}, {'templateQuery': '<How> does <Judy> <organize> their time?'}, {'templateQuery': '<How> does <Judy> <allocate> their time?'}, {'templateQuery': '<How> does <Judy> <manage> their time?'}, {'templateQuery': '<How> does <Judy> <spent> their time?'}, {'templateQuery': '<How> does <Judy> <spend> their time?'}, {'templateQuery': '<How> does <Judy> <organize> their time?'}, {'templateQuery': '<How> does <Mac> <organize> their time?'}, {'templateQuery': '<How> does <Mac> <allocate> their time?'}, {'templateQuery': '<How> does <Mac> <manage> their time?'}, {'templateQuery': '<How> does <Mac> <spent> their time?'}, {'templateQuery': '<How> does <Mac> <spend> their time?'}, {'templateQuery': '<How> does <Mac> <organize> their time?'}]
48

For those who are curious, here is a solution with the cartesian product generator implemented from scratch:

def product(d, _l, current = []):
   if len(current) == _l:
      yield current
   else:
      for i in d[0]:
        yield from product(d[1:], _l, current+[i])

_combos = list(product(all_aliases, len(all_aliases)))
new_t = [(lambda c:{'templateQuery':re.sub('(?<=\<).*?(?=\>)', lambda x:next(c), t['templateQuery'])})(iter(i)) \
   for i in _combos]

print(len(new_t))

Output:

[{'templateQuery': '<how> does <Peter> <organize> their time?'}, {'templateQuery': '<how> does <Peter> <allocate> their time?'}, {'templateQuery': '<how> does <Peter> <manage> their time?'}, {'templateQuery': '<how> does <Peter> <spent> their time?'}, {'templateQuery': '<how> does <Peter> <spend> their time?'}, {'templateQuery': '<how> does <Peter> <organize> their time?'}, {'templateQuery': '<how> does <Julie> <organize> their time?'}, {'templateQuery': '<how> does <Julie> <allocate> their time?'}, {'templateQuery': '<how> does <Julie> <manage> their time?'}, {'templateQuery': '<how> does <Julie> <spent> their time?'}, {'templateQuery': '<how> does <Julie> <spend> their time?'}, {'templateQuery': '<how> does <Julie> <organize> their time?'}, {'templateQuery': '<how> does <Judy> <organize> their time?'}, {'templateQuery': '<how> does <Judy> <allocate> their time?'}, {'templateQuery': '<how> does <Judy> <manage> their time?'}, {'templateQuery': '<how> does <Judy> <spent> their time?'}, {'templateQuery': '<how> does <Judy> <spend> their time?'}, {'templateQuery': '<how> does <Judy> <organize> their time?'}, {'templateQuery': '<how> does <Mac> <organize> their time?'}, {'templateQuery': '<how> does <Mac> <allocate> their time?'}, {'templateQuery': '<how> does <Mac> <manage> their time?'}, {'templateQuery': '<how> does <Mac> <spent> their time?'}, {'templateQuery': '<how> does <Mac> <spend> their time?'}, {'templateQuery': '<how> does <Mac> <organize> their time?'}, {'templateQuery': '<How> does <Peter> <organize> their time?'}, {'templateQuery': '<How> does <Peter> <allocate> their time?'}, {'templateQuery': '<How> does <Peter> <manage> their time?'}, {'templateQuery': '<How> does <Peter> <spent> their time?'}, {'templateQuery': '<How> does <Peter> <spend> their time?'}, {'templateQuery': '<How> does <Peter> <organize> their time?'}, {'templateQuery': '<How> does <Julie> <organize> their time?'}, {'templateQuery': '<How> does <Julie> <allocate> their time?'}, {'templateQuery': '<How> does <Julie> <manage> their time?'}, {'templateQuery': '<How> does <Julie> <spent> their time?'}, {'templateQuery': '<How> does <Julie> <spend> their time?'}, {'templateQuery': '<How> does <Julie> <organize> their time?'}, {'templateQuery': '<How> does <Judy> <organize> their time?'}, {'templateQuery': '<How> does <Judy> <allocate> their time?'}, {'templateQuery': '<How> does <Judy> <manage> their time?'}, {'templateQuery': '<How> does <Judy> <spent> their time?'}, {'templateQuery': '<How> does <Judy> <spend> their time?'}, {'templateQuery': '<How> does <Judy> <organize> their time?'}, {'templateQuery': '<How> does <Mac> <organize> their time?'}, {'templateQuery': '<How> does <Mac> <allocate> their time?'}, {'templateQuery': '<How> does <Mac> <manage> their time?'}, {'templateQuery': '<How> does <Mac> <spent> their time?'}, {'templateQuery': '<How> does <Mac> <spend> their time?'}, {'templateQuery': '<How> does <Mac> <organize> their time?'}]
 48

Upvotes: 2

Related Questions