我有一本字典,
ex_dict={'recency': ['current',
'savings',
'fixed',
'current',
'savings',
'fixed',
'current',
'fixed',
'fixed',
'fixed',
'current',
'fixed'],
'frequency': ['freq',
'freq',
'freq',
'freq',
'freq',
'freq',
'infreq',
'freq',
'freq',
'freq',
'infreq',
'freq'],
'money': ['med',
'high',
'high',
'med',
'high',
'high',
'low',
'high',
'md',
'high',
'high',
'high']}
另一本字典,
cond_dict= {'recency': {'current': 0.33, 'fixed': 0.5},
'frequency': {'freq': 0.83},
'money': {'high': 0.67}}
在这里,我想在ex_dict中填充值列表,如果它的元素存在于字典cond_dict的键中。
例如:
在字典ex_dict中,有一个名为“ recency”的键,它有一个包含12个元素的列表,这里有3个唯一元素,例如(当前,储蓄,固定)。
这三个元素应存在于字典的cond_dict键中,如果此dict键中不存在任何元素,则其值应作为'RARE'添加到与ex_dict相关的列表中。
这是一个示例输出:原始列表中的节省被RARE替换,因为cond_dict关键字中不存在节省。
'recency': ['current',
'RARE',
'fixed',
'current',
'RARE',
'fixed',
'current',
'fixed',
'fixed',
'fixed',
'current',
'fixed']
您能在上面写下您的建议/答案吗?
for k,v in ex_dict.items():
ex_dict[k] = [item if item in cond_dict[k] else 'RARE' for item in v]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句