python用所有可能的组合用多个值替换子字符串

时间:2018-11-09 23:27:24

标签: python string replace combinations

我要实现的目标:

  {
    "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个变体。

1 个答案:

答案 0 :(得分:2)

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