好的,所以我有两本词典.
dictionary_1 = {'status': ['online', 'Away', 'Offline'],
'Absent':['yes', 'no', 'half day']}
dictionary_2 = {'healthy': ['yes', 'no'],
'insane': ['yes', 'no']
Run Code Online (Sandbox Code Playgroud)
现在我需要将它们组合起来,以便我得到一个新的字典:
{'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']
}
Run Code Online (Sandbox Code Playgroud)
这是一个很晚的更新,但如果有人感兴趣,我找到了一种没有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: …Run Code Online (Sandbox Code Playgroud) python dictionary cartesian-product python-itertools python-3.x