さて、私は2つの辞書を持っています。
dictionary_1 = {'status': ['online', 'Away', 'Offline'],
'Absent':['yes', 'no', 'half day']}
dictionary_2 = {'healthy': ['yes', 'no'],
'insane': ['yes', 'no']
今、私はそれらを組み合わせて、新しい辞書を取得する必要があります:
{'status': ['online', 'online', 'away', 'away', 'Offline', 'Offline'],
'Absent': ['yes', 'yes', 'no', 'no', 'half day', 'half day'],
'healthy': ['yes', 'no', 'yes', 'no', 'yes', 'no'],
'insane': ['yes', 'no', 'yes', 'no', 'yes', 'no']
}
これは非常に遅い更新ですが、誰かが興味を持っている場合は itertools なしでそれを行う方法を見つけました。
def cartesian_product(dict1, dict2):
cartesian_dict = {}
dict1_length = len(list(dict1.values())[0])
dict2_length = len(list(dict2.values())[0])
h = []
for key in dict1:
for value in dict1[key]:
if not key in cartesian_dict:
cartesian_dict[key] = []
cartesian_dict[key].extend([value]*dict2_length)
else:
cartesian_dict[key].extend([value]*dict2_length)
for key in dict2:
cartesian_dict[key] = dict2[key]*dict1_length
return cartesian_dict