Я столкнулся с этой проблемой некоторое время назад, делая тестовые примеры. У меня есть пакет на pip, который теперь называется «looper», который расширяет itertools с некоторой магией словаря и другими вещами, которые я нашел полезными.
https://pypi.python.org/pypi/looper
То, что вы хотите, не похоже на полное декартово произведение двух словарей, которое будет состоять из 36 элементов, объединяющих каждый ключ d1 [k1] * d1 [k2] * d2 [k1] * d2 [k2].
Вместо этого вы, кажется, хотите d1[k1,k2] * d2[k1,k2], равномерно перебирая n для каждого ключа. Это известно как функция zip, и dict_zip делает это для словарей.
from pprint import pprint
from looper import iterutil
dict_1 = {'status': ['online', 'Away', 'Offline'],
'Absent': ['yes', 'no', 'half day']}
dict_2 = {'healthy': ['yes', 'no'],
'insane': ['yes', 'no']}
# the first thing to do is to zip the dictionaries up. This produces a dictionary for each value of n in d[k][n]
zipped_dict_1 = iterutil.dict_zip(**dict_1)
# {'Absent': 'yes', 'status': 'online'}
# {'Absent': 'no', 'status': 'Away'}
# {'Absent': 'half day', 'status': 'Offline'}
zipped_dict_2 = iterutil.dict_zip(**dict_2)
# {'healthy': 'yes', 'insane': 'yes'}
# {'healthy': 'no', 'insane': 'no'}
# Now the output is a list of flattened dictionaries, take the Cartesian product of them.
product_dict = iterutil.product(zipped_dict_1,zipped_dict_2)
# ({'Absent': 'yes', 'status': 'online'}, {'healthy': 'yes', 'insane': 'yes'})
# ({'Absent': 'yes', 'status': 'online'}, {'healthy': 'no', 'insane': 'no'})
# ({'Absent': 'no', 'status': 'Away'}, {'healthy': 'yes', 'insane': 'yes'})
# ({'Absent': 'no', 'status': 'Away'}, {'healthy': 'no', 'insane': 'no'})
# ({'Absent': 'half day', 'status': 'Offline'}, {'healthy': 'yes', 'insane': 'yes'})
# ({'Absent': 'half day', 'status': 'Offline'}, {'healthy': 'no', 'insane': 'no'})
# The product function produces tuples which must be combined in to a final dictionary.
# Merge the dictionaries using imap
merged_dict = iterutil.imap(lambda x: dict(x[0].items()+x[1].items()),product_dict)
for d in merged_dict:
pprint(d)
ВЫХОД
{'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
{'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
{'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
{'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
{'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
{'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
person
Syncryptic
schedule
03.04.2014
li
, заархивированные сами с собой, ваши вторые два объединяют 3 копииli
. Какой ты хочешь? - person roippi   schedule 13.11.2013'online'
? Вы пытаетесь получить копию каждогоstatus
для каждого членаzip(healthy, insane)
, копию каждогоAbsent
для каждого членаzip(healthy, insane)
, копию каждогоhealthy
для каждого членаzip(status, Absent)
и копию каждогоinsane
для каждого членаzip(status, Absent)
? - person abarnert   schedule 13.11.2013