Вы можете использовать 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