代码之家  ›  专栏  ›  技术社区  ›  aerin

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

  •  1
  • aerin  · 技术社区  · 7 年前

      {
        "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 回复  |  直到 7 年前
        1
  •  2
  •   Ajax1234    7 年前

    你可以用 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