本文介绍了笛卡尔产品用于两个字典python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,我有两个字典。 dictionary_1 = {'status':['online','Away','Offline'],
'缺席':['yes','no','half day']}
dictionary_2 = {'healthy':['yes','no'],
'insane':['yes ','no']
现在我需要组合它们,以便我得到一个新的字典:
{'status':['online','online','away','away','Offline' '离线'],
'缺席':['yes','yes','no','no','half day','half day'],
'healthy' '是','不','是','不','是','不'],
'疯狂':['是','不','是' '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 = [ ]
在dict1中的键值:
在dict1中的值[key]:
如果不是在cartesian_dict中键:
cartesian_dict [key] = []
cartesian_dict [key ] .extend([value] * dict2_length)
else:
cartesian_dict [key] .extend([value] * dict2_length)
for dict2:
cartesian_dict [key] = dict2 [key] * dict1_length
return cartesian_dict
解决方案
根据@ abarnert的解释(并假设当前输出中的健康
和 insane
值是最好的猜测错误,因为他们只有四个成员):
d1 = {'status':['online','Away' 'offline'],'absent':['yes','no','half day']}
d2 = {'h ealthy':['yes','no'],'insane':['yes','no']}
d1_columns = zip(* d1.values())
d2_columns = * d2.values())
col_groups = [c1 + c2 for c1,c2 in itertools.product(d1_columns,d2_columns)]
rows = zip(* col_groups)
combined_keys = list d1)+ list(d2)
d_combined = dict(zip(combined_keys,rows))
>>> pprint.pprint(d_combined)
{'缺席':('是','是','不','不','半天','半天'),
' :('是','不','是','不','是','否'),
'insane':('是','否','是' ''''''''''','''''''''('在线','在线','离开','离开','离线')}
或在您的订单中
>>> order = [status,absent,healthy,insane]
>>>对于k按顺序:
print k,d_combined [k]
...
status('online','online','Away','Away','Offline','离线')
缺席('是','是','不','不','半天','半天')
健康('是','不'是的,'不','是','否')
疯狂('是','不','是','否','是' / code>
ok so i've got two dictionaries.
dictionary_1 = {'status': ['online', 'Away', 'Offline'],
'Absent':['yes', 'no', 'half day']}
dictionary_2 = {'healthy': ['yes', 'no'],
'insane': ['yes', 'no']
Now i need to combine them so that i get a new dictionary with:
{'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']
}
This is an update which is very late but I found a way to do it without itertools if anyone is interested.
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
解决方案
Best guess, based on @abarnert's interpretation (and assuming that the
healthy
and insane
values in the current output are wrong, as they only have four members):
d1 = {'status': ['online', 'Away', 'Offline'] ,'absent':['yes', 'no', 'half day']}
d2 = {'healthy': ['yes', 'no'], 'insane': ['yes', 'no']}
d1_columns = zip(*d1.values())
d2_columns = zip(*d2.values())
col_groups = [c1+c2 for c1, c2 in itertools.product(d1_columns, d2_columns)]
rows = zip(*col_groups)
combined_keys = list(d1) + list(d2)
d_combined = dict(zip(combined_keys, rows))
which produces
>>> pprint.pprint(d_combined)
{'absent': ('yes', 'yes', 'no', 'no', 'half day', 'half day'),
'healthy': ('yes', 'no', 'yes', 'no', 'yes', 'no'),
'insane': ('yes', 'no', 'yes', 'no', 'yes', 'no'),
'status': ('online', 'online', 'Away', 'Away', 'Offline', 'Offline')}
or, in your order,
>>> order = ["status", "absent", "healthy", "insane"]
>>> for k in order:
print k, d_combined[k]
...
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')
这篇关于笛卡尔产品用于两个字典python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!