Я столкнулся с этой проблемой некоторое время назад, создавая тестовые примеры. У меня есть пакет на pip, который теперь называется «looper», который расширяет itertools с помощью некоторой магии словаря и других вещей, которые я нашел полезными.
https://pypi.python.org/pypi/looper
То, что вы хотите, кажется, не быть полным декартово произведение двух dictionarys, который будет долго 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'}
Ваш первый два уплощение 'li' заархивированы с собой, ваш второй два конкатенации 3 экземпляра' li'. Какой ты хочешь? – roippi
Как вы получаете три копии «онлайн»? Вы пытаетесь получить копию каждого 'статуса' для каждого члена' zip (здорового, безумного) ', копии каждого' Absent' для каждого члена 'zip (здорового, безумного)', копии каждого ' здоровый' для каждого члена 'zip (status, Absent)' и копия каждого 'insane' для каждого члена' zip (status, Absent) '? – abarnert
Пожалуйста, объясните свой декартовый продукт, ваш ожидаемый результат не так очевиден. –