【发布时间】:2017-04-13 04:25:53
【问题描述】:
我有 3 部字典:
dict1 = {'Name1':'Andrew','Name2':'Kevin'....'NameN':'NameN'}- this would have maximum 20 names
dict2 = {'Travel':'Andrew','Footbal':'Kevin',...'Nhobby':'NameN'}- this would have as many names as there are in dict1
dict3 = {'Travel':'ID01','Footbal':'ID02','Travel':'ID03','Photo':'ID04','Footbal':'ID05','Photo':'ID06','Climbing':'ID07'....}
我想合并这 3 个字典,以便第 3 个以这种方式结束:
dict3 = {'Andrew':'ID01','Kevin':'ID02','Andrew':'ID03','Kevin':'ID04','Kevin':'ID05','Kevin':'ID06','Andrew':'ID07',....}. Basically the hobbies that are in the dict 2 will be kept while the remaining hobbies will be split among the total number of names with a +1 in the case of an uneven number of hobbies.
我已经从这里Merge dictionaries retaining values for duplicate keys 尝试了合并功能,但我很难将 dict3 平均分配给所有名称。
【问题讨论】:
-
您需要的输出是不可能的。字典必须有唯一的键。
标签: python pandas dictionary merge split