首页 > 解决方案 > python用所有可能的组合替换具有多个值的子字符串

问题描述

我想要达到的目标:

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

我想<id>用所有可能的值组合替换每个键<aliases>。因此,它可以构成一个像How does Peter manage their time?

例如,

  {
    "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"
  },

在 python 中实现这一目标的最佳方法是什么?我想过使用模板(https://docs.python.org/dev/library/string.html#template-strings)但是,它只能处理一个候选人。对我来说,我想替换所有可能的组合,在这个例子中是 2 * 4 * 6 = 48 种变化。

标签: pythonstringreplacecombinations

解决方案


您可以使用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))

输出:

[{'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

对于那些好奇的人,这里有一个从头开始实现笛卡尔积生成器的解决方案:

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))

输出:

[{'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

推荐阅读