Изготовление продукта из предметов в словарях - PullRequest
0 голосов
/ 16 апреля 2020

скажем, у меня есть этот список состояний:

states1 = ['one', 'two', 'four']
states2 = ['far away', 'normal', 'to close']
states3 = ['thor', 'iron man']

, и я хочу сделать из них продукт

import itertools

state_indexes = [s for s in itertools.product(states1, states2, states3)]
print(state_indexes)

"""
[('one', 'far away', 'thor'), ('one', 'far away', 'iron man'), ('one', 'normal', 'thor'), ('one', 'normal', 'iron man'), ('one', 'to close', 'thor'), ('one', 'to close', 'iron man'), ('two', 'far away', 'thor'), ('two', 'far away', 'iron man'), ('two', 'normal', 'thor'), ('two', 'normal', 'iron man'), ('two', 'to close', 'thor'), ('two', 'to close', 'iron man'), ('four', 'far away', 'thor'), ('four', 'far away', 'iron man'), ('four', 'normal', 'thor'), ('four', 'normal', 'iron man'), ('four', 'to close', 'thor'), ('four', 'to close', 'iron 
man')]
"""

, но это жестко закодированный способ сделать это, Я хочу создать список состояний автоматически, поэтому я создал этот словарь:

my_dictionary = {}

for i in range(10):
    my_dictionary[f"item_{i}"] = ['state1', 'state2']

"""
{'item_0': ['state1', 'state2'], 'item_1': ['state1', 'state2'], 'item_2': ['state1', 'state2'], 'item_3': ['state1', 'state2'], 'item_4': ['state1', 'state2'], 'item_5': ['state1', 'state2'], 'item_6': ['state1', 'state2'], 'item_7': ['state1', 'state2'], 'item_8': ['state1', 'state2'], 'item_9': ['state1', 'state2']}
"""

До сих пор он работает, но как я могу воссоздать список state_indexes?

Это один из подходов Я пытался:

state_indexes = [s for s in itertools.product(my_dictionary)]
print(state_indexes)
"""
[('item_0',), ('item_1',), ('item_2',), ('item_3',), ('item_4',), ('item_5',), ('item_6',), ('item_7',), ('item_8',), ('item_9',)]
"""

Как я могу это исправить? Большое спасибо

РЕДАКТИРОВАТЬ: Ожидаемый результат в основном это (как указано в примере выше):

"""
[('one', 'far away', 'thor'), ('one', 'far away', 'iron man'), ('one', 'normal', 'thor'), ('one', 'normal', 'iron man'), ('one', 'to close', 'thor'), ('one', 'to close', 'iron man'), ('two', 'far away', 'thor'), ('two', 'far away', 'iron man'), ('two', 'normal', 'thor'), ('two', 'normal', 'iron man'), ('two', 'to close', 'thor'), ('two', 'to close', 'iron man'), ('four', 'far away', 'thor'), ('four', 'far away', 'iron man'), ('four', 'normal', 'thor'), ('four', 'normal', 'iron man'), ('four', 'to close', 'thor'), ('four', 'to close', 'iron 
man')]
"""

Так что это будет выглядеть так (я знаю, что это не очень интуитивно понятно Вот почему я использовал пример)

"""
[('state1', 'state1', 'state1',...,'state1'), ('state1', 'state1',..., 'state2'),..., ('state2', 'state2',... ,'state2')]
"""

1 Ответ

2 голосов
/ 16 апреля 2020

Для произведения всех списков состояний вам придется распаковать значения dict, используя оператор *:

state_indexes = [s for s in itertools.product(*my_dictionary.values())]
# state_indexes = list(itertools.product(*my_dictionary.values()))

Обратите внимание, однако, что порядок значений dict не гарантируется во всех Python версиях. Возможно, вам придется использовать list или collections.OrderedDict.

...